+ return (result | m_detected) & flags;
+}
+
+// All Wait functions poll the socket using GSocket_Select() to
+// check for the specified combination of conditions, until one
+// of these conditions become true, an error occurs, or the
+// timeout elapses. The polling loop runs the event loop so that
+// this won't block the GUI.
+
+bool
+wxSocketBase::DoWait(long seconds, long milliseconds, wxSocketEventFlags flags)
+{
+ wxCHECK_MSG( m_socket, false, "can't wait on invalid socket" );
+
+ // This can be set to true from Interrupt() to exit this function a.s.a.p.
+ m_interrupt = false;
+
+
+ // Use either the provided timeout or the default timeout value associated
+ // with this socket.
+ //
+ // TODO: allow waiting forever, see #9443
+ const long timeout = seconds == -1 ? m_timeout * 1000
+ : seconds * 1000 + milliseconds;
+ const wxMilliClock_t timeEnd = wxGetLocalTimeMillis() + timeout;
+
+ // Get the active event loop which we'll use for the message dispatching
+ // when running in the main thread
+ wxEventLoopBase *eventLoop;
+ if ( wxIsMainThread() )
+ {
+ eventLoop = wxEventLoop::GetActive();
+ }
+ else // in worker thread
+ {
+ // We never dispatch messages from threads other than the main one.
+ eventLoop = NULL;
+ }
+
+ // Wait in an active polling loop: notice that the loop is executed at
+ // least once, even if timeout is 0 (i.e. polling).
+ bool gotEvent = false;
+ for ( ;; )
+ {
+ // We always stop waiting when the connection is lost as it doesn't
+ // make sense to continue further, even if GSOCK_LOST_FLAG is not
+ // specified in flags to wait for.
+ const GSocketEventFlags
+ result = m_socket->Select(flags | GSOCK_LOST_FLAG);
+
+ // Incoming connection (server) or connection established (client)?
+ if ( result & GSOCK_CONNECTION_FLAG )
+ {
+ m_connected = true;
+ m_establishing = false;
+ gotEvent = true;
+ break;
+ }
+
+ // Data available or output buffer ready?
+ if ( (result & GSOCK_INPUT_FLAG) || (result & GSOCK_OUTPUT_FLAG) )
+ {
+ gotEvent = true;
+ break;
+ }
+
+ // Connection lost
+ if ( result & GSOCK_LOST_FLAG )
+ {
+ m_connected = false;
+ m_establishing = false;
+ if ( flags & GSOCK_LOST_FLAG )
+ gotEvent = true;
+ break;
+ }