+void wxSocketImpl::PostCreation()
+{
+ // FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option
+#ifdef SO_NOSIGPIPE
+ EnableSocketOption(SO_NOSIGPIPE);
+#endif
+
+ if ( m_reusable )
+ EnableSocketOption(SO_REUSEADDR);
+
+ if ( m_broadcast )
+ {
+ wxASSERT_MSG( !m_stream, "broadcasting is for datagram sockets only" );
+
+ EnableSocketOption(SO_BROADCAST);
+ }
+
+ if ( m_initialRecvBufferSize >= 0 )
+ SetSocketOption(SO_RCVBUF, m_initialRecvBufferSize);
+ if ( m_initialSendBufferSize >= 0 )
+ SetSocketOption(SO_SNDBUF, m_initialSendBufferSize);
+
+ // we always put our sockets in unblocked mode and handle blocking
+ // ourselves in DoRead/Write() if wxSOCKET_WAITALL is specified
+ UnblockAndRegisterWithEventLoop();
+}
+
+wxSocketError wxSocketImpl::UpdateLocalAddress()
+{
+ if ( !m_local.IsOk() )
+ {
+ // ensure that we have a valid object using the correct family: correct
+ // being the same one as our peer uses as we have no other way to
+ // determine it
+ m_local.Create(m_peer.GetFamily());
+ }
+
+ WX_SOCKLEN_T lenAddr = m_local.GetLen();
+ if ( getsockname(m_fd, m_local.GetWritableAddr(), &lenAddr) != 0 )
+ {
+ Close();
+ m_error = wxSOCKET_IOERR;
+ return m_error;
+ }
+
+ return wxSOCKET_NOERROR;
+}
+
+wxSocketError wxSocketImpl::CreateServer()
+{
+ if ( !PreCreateCheck(m_local) )
+ return m_error;
+
+ m_server = true;
+ m_stream = true;
+
+ // do create the socket
+ m_fd = socket(m_local.GetFamily(), SOCK_STREAM, 0);
+
+ if ( m_fd == INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_IOERR;
+ return wxSOCKET_IOERR;
+ }
+
+ PostCreation();
+
+ // and then bind to and listen on it
+ //
+ // FIXME: should we test for m_dobind here?
+ if ( bind(m_fd, m_local.GetAddr(), m_local.GetLen()) != 0 )
+ m_error = wxSOCKET_IOERR;
+
+ if ( IsOk() )
+ {
+ if ( listen(m_fd, 5) != 0 )
+ m_error = wxSOCKET_IOERR;
+ }
+
+ if ( !IsOk() )
+ {
+ Close();
+ return m_error;
+ }
+
+ // finally retrieve the address we effectively bound to
+ return UpdateLocalAddress();
+}
+
+wxSocketError wxSocketImpl::CreateClient(bool wait)
+{
+ if ( !PreCreateCheck(m_peer) )
+ return m_error;
+
+ m_fd = socket(m_peer.GetFamily(), SOCK_STREAM, 0);
+
+ if ( m_fd == INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_IOERR;
+ return wxSOCKET_IOERR;
+ }
+
+ PostCreation();
+
+ // 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()