+ // If a local address has been set, then bind to it before calling connect
+ if ( m_local.IsOk() )
+ {
+ if ( bind(m_fd, m_local.GetAddr(), m_local.GetLen()) != 0 )
+ {
+ Close();
+ m_error = wxSOCKET_IOERR;
+ return m_error;
+ }
+ }
+
+ // Do connect now
+ int rc = connect(m_fd, m_peer.GetAddr(), m_peer.GetLen());
+ if ( rc == SOCKET_ERROR )
+ {
+ wxSocketError err = GetLastError();
+ if ( err == wxSOCKET_WOULDBLOCK )
+ {
+ m_establishing = true;
+
+ // block waiting for connection if we should (otherwise just return
+ // wxSOCKET_WOULDBLOCK to the caller)
+ if ( wait )
+ {
+ err = SelectWithTimeout(wxSOCKET_CONNECTION_FLAG)
+ ? wxSOCKET_NOERROR
+ : wxSOCKET_TIMEDOUT;
+ m_establishing = false;
+ }
+ }
+
+ m_error = err;
+ }
+ else // connected
+ {
+ m_error = wxSOCKET_NOERROR;
+ }
+
+ return m_error;
+}
+
+
+wxSocketError wxSocketImpl::CreateUDP()
+{
+ if ( !PreCreateCheck(m_local) )
+ return m_error;
+
+ m_stream = false;
+ m_server = false;
+
+ m_fd = socket(m_local.GetFamily(), SOCK_DGRAM, 0);
+
+ if ( m_fd == INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_IOERR;
+ return wxSOCKET_IOERR;
+ }
+
+ PostCreation();
+
+ if ( m_dobind )
+ {
+ if ( bind(m_fd, m_local.GetAddr(), m_local.GetLen()) != 0 )
+ {
+ Close();
+ m_error = wxSOCKET_IOERR;
+ return m_error;
+ }
+
+ return UpdateLocalAddress();
+ }
+
+ return wxSOCKET_NOERROR;
+}
+
+wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket)
+{
+ wxSockAddressStorage from;
+ WX_SOCKLEN_T fromlen = sizeof(from);
+ const SOCKET fd = accept(m_fd, &from.addr, &fromlen);
+
+ // accepting is similar to reading in the sense that it resets "ready for
+ // read" flag on the socket
+ ReenableEvents(wxSOCKET_INPUT_FLAG);
+
+ if ( fd == INVALID_SOCKET )
+ return NULL;
+
+ wxSocketManager * const manager = wxSocketManager::Get();
+ if ( !manager )
+ return NULL;
+
+ wxSocketImpl * const sock = manager->CreateSocket(wxsocket);
+ if ( !sock )
+ return NULL;
+
+ sock->m_fd = fd;
+ sock->m_peer = wxSockAddressImpl(from.addr, fromlen);
+
+ sock->UnblockAndRegisterWithEventLoop();
+
+ return sock;
+}
+
+
+void wxSocketImpl::Close()
+{
+ if ( m_fd != INVALID_SOCKET )
+ {
+ DoClose();
+ m_fd = INVALID_SOCKET;
+ }
+}
+
+void wxSocketImpl::Shutdown()
+{
+ if ( m_fd != INVALID_SOCKET )
+ {
+ shutdown(m_fd, 1 /* SD_SEND */);
+ Close();
+ }
+}
+
+/*
+ * Sets the timeout for blocking calls. Time is expressed in
+ * milliseconds.
+ */
+void wxSocketImpl::SetTimeout(unsigned long millis)
+{
+ SetTimeValFromMS(m_timeout, millis);
+}
+
+void wxSocketImpl::NotifyOnStateChange(wxSocketNotify event)
+{
+ m_wxsocket->OnRequest(event);
+}
+
+/* Address handling */
+wxSocketError wxSocketImpl::SetLocal(const wxSockAddressImpl& local)
+{
+ /* the socket must be initialized, or it must be a server */
+ if (m_fd != INVALID_SOCKET && !m_server)
+ {
+ m_error = wxSOCKET_INVSOCK;
+ return wxSOCKET_INVSOCK;
+ }
+
+ if ( !local.IsOk() )
+ {
+ m_error = wxSOCKET_INVADDR;
+ return wxSOCKET_INVADDR;
+ }
+
+ m_local = local;
+
+ return wxSOCKET_NOERROR;
+}
+
+wxSocketError wxSocketImpl::SetPeer(const wxSockAddressImpl& peer)
+{
+ if ( !peer.IsOk() )
+ {
+ m_error = wxSOCKET_INVADDR;
+ return wxSOCKET_INVADDR;
+ }
+
+ m_peer = peer;
+
+ return wxSOCKET_NOERROR;
+}
+
+const wxSockAddressImpl& wxSocketImpl::GetLocal()
+{
+ if ( !m_local.IsOk() )
+ UpdateLocalAddress();
+
+ return m_local;
+}
+
+// ----------------------------------------------------------------------------
+// wxSocketImpl IO
+// ----------------------------------------------------------------------------
+
+// this macro wraps the given expression (normally a syscall) in a loop which
+// ignores any interruptions, i.e. reevaluates it again if it failed and errno
+// is EINTR
+#ifdef __UNIX__
+ #define DO_WHILE_EINTR( rc, syscall ) \
+ do { \
+ rc = (syscall); \
+ } \
+ while ( rc == -1 && errno == EINTR )