+}
+
+bool wxSocketImpl::PreCreateCheck(const wxSockAddressImpl& addr)
+{
+ if ( m_fd != INVALID_SOCKET )
+ {
+ m_error = wxSOCKET_INVSOCK;
+ return false;
+ }
+
+ if ( !addr.IsOk() )
+ {
+ m_error = wxSOCKET_INVADDR;
+ return false;
+ }
+
+ return true;
+}
+
+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()
+{
+ 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;
+ }
+ }