+ if ( m_fd == INVALID_SOCKET )
+ return (wxSOCKET_LOST_FLAG & flags);
+
+ struct timeval tv;
+ if ( timeout )
+ tv = *timeout;
+ else
+ tv.tv_sec = tv.tv_usec = 0;
+
+ // prepare the FD sets, passing NULL for the one(s) we don't use
+ fd_set
+ readfds, *preadfds = NULL,
+ writefds, *pwritefds = NULL,
+ exceptfds; // always want to know about errors
+
+ if ( flags & wxSOCKET_INPUT_FLAG )
+ {
+ preadfds = &readfds;
+ wxFD_ZERO(preadfds);
+ wxFD_SET(m_fd, preadfds);
+ }
+
+ // when using non-blocking connect() the socket becomes connected
+ // (successfully or not) when it becomes writable
+ if ( flags & (wxSOCKET_OUTPUT_FLAG | wxSOCKET_CONNECTION_FLAG) )
+ {
+ pwritefds = &writefds;
+ wxFD_ZERO(pwritefds);
+ wxFD_SET(m_fd, pwritefds);
+ }
+
+ wxFD_ZERO(&exceptfds);
+ wxFD_SET(m_fd, &exceptfds);
+
+ const int rc = select(m_fd + 1, preadfds, pwritefds, &exceptfds, &tv);
+
+ // check for errors first
+ if ( rc == -1 || wxFD_ISSET(m_fd, &exceptfds) )
+ {
+ m_establishing = false;
+
+ return wxSOCKET_LOST_FLAG & flags;
+ }
+
+ if ( rc == 0 )
+ return 0;
+
+ wxASSERT_MSG( rc == 1, "unexpected select() return value" );
+
+ wxSocketEventFlags detected = 0;
+ if ( preadfds && wxFD_ISSET(m_fd, preadfds) )
+ detected |= wxSOCKET_INPUT_FLAG;
+
+ if ( pwritefds && wxFD_ISSET(m_fd, pwritefds) )
+ {
+ // check for the case of non-blocking connect()
+ if ( m_establishing && !m_server )
+ {
+ int error;
+ SOCKOPTLEN_T len = sizeof(error);
+ m_establishing = false;
+ getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
+
+ if ( error )
+ detected = wxSOCKET_LOST_FLAG;
+ else
+ detected |= wxSOCKET_CONNECTION_FLAG;
+ }
+ else // not called to get non-blocking connect() status
+ {
+ detected |= wxSOCKET_OUTPUT_FLAG;
+ }
+ }