+// wxSocketWaitModeChanger: temporarily change the socket flags affecting its
+// wait mode
+class wxSocketWaitModeChanger
+{
+public:
+    // temporarily set the flags to include the flag value which may be either
+    // wxSOCKET_NOWAIT or wxSOCKET_WAITALL
+    wxSocketWaitModeChanger(wxSocketBase *socket, int flag)
+        : m_socket(socket),
+          m_oldflags(socket->GetFlags())
+
+    {
+        // We can be passed only wxSOCKET_WAITALL{_READ,_WRITE} or
+        // wxSOCKET_NOWAIT{_READ,_WRITE} normally.
+        wxASSERT_MSG( !(flag & wxSOCKET_WAITALL) || !(flag & wxSOCKET_NOWAIT),
+                      "not a wait flag" );
+
+        // preserve wxSOCKET_BLOCK value when switching to wxSOCKET_WAITALL
+        // mode but not when switching to wxSOCKET_NOWAIT as the latter is
+        // incompatible with wxSOCKET_BLOCK
+        if ( flag != wxSOCKET_NOWAIT )
+            flag |= m_oldflags & wxSOCKET_BLOCK;
+
+        socket->SetFlags(flag);
+    }
+
+    ~wxSocketWaitModeChanger()
+    {
+        m_socket->SetFlags(m_oldflags);
+    }
+
+private:
+    wxSocketBase * const m_socket;
+    const int m_oldflags;
+
+    wxDECLARE_NO_COPY_CLASS(wxSocketWaitModeChanger);
+};
+
+// wxSocketRead/WriteGuard are instantiated before starting reading
+// from/writing to the socket
+class wxSocketReadGuard
+{
+public:
+    wxSocketReadGuard(wxSocketBase *socket)
+        : m_socket(socket)
+    {
+        wxASSERT_MSG( !m_socket->m_reading, "read reentrancy?" );
+
+        m_socket->m_reading = true;
+    }
+
+    ~wxSocketReadGuard()
+    {
+        m_socket->m_reading = false;
+
+        // connection could have been lost while reading, in this case calling
+        // ReenableEvents() would assert and is not necessary anyhow
+        wxSocketImpl * const impl = m_socket->m_impl;
+        if ( impl && impl->m_fd != INVALID_SOCKET )
+            impl->ReenableEvents(wxSOCKET_INPUT_FLAG);
+    }
+
+private:
+    wxSocketBase * const m_socket;
+
+    wxDECLARE_NO_COPY_CLASS(wxSocketReadGuard);
+};
+
+class wxSocketWriteGuard
+{
+public:
+    wxSocketWriteGuard(wxSocketBase *socket)
+        : m_socket(socket)
+    {
+        wxASSERT_MSG( !m_socket->m_writing, "write reentrancy?" );
+
+        m_socket->m_writing = true;
+    }
+
+    ~wxSocketWriteGuard()
+    {
+        m_socket->m_writing = false;
+
+        wxSocketImpl * const impl = m_socket->m_impl;
+        if ( impl && impl->m_fd != INVALID_SOCKET )
+            impl->ReenableEvents(wxSOCKET_OUTPUT_FLAG);
+    }
+
+private:
+    wxSocketBase * const m_socket;
+
+    wxDECLARE_NO_COPY_CLASS(wxSocketWriteGuard);
+};
+
+// ============================================================================
+// wxSocketManager
+// ============================================================================
+
+wxSocketManager *wxSocketManager::ms_manager = NULL;
+
+/* static */
+void wxSocketManager::Set(wxSocketManager *manager)
+{
+    wxASSERT_MSG( !ms_manager, "too late to set manager now" );
+
+    ms_manager = manager;
+}
+
+/* static */
+void wxSocketManager::Init()
+{
+    wxASSERT_MSG( !ms_manager, "shouldn't be initialized twice" );
+
+    /*
+        Details: Initialize() creates a hidden window as a sink for socket
+        events, such as 'read completed'. wxMSW has only one message loop
+        for the main thread. If Initialize is called in a secondary thread,
+        the socket window will be created for the secondary thread, but
+        since there is no message loop on this thread, it will never
+        receive events and all socket operations will time out.
+        BTW, the main thread must not be stopped using sleep or block
+        on a semaphore (a bad idea in any case) or socket operations
+        will time out.
+
+        On the Mac side, Initialize() stores a pointer to the CFRunLoop for
+        the main thread. Because secondary threads do not have run loops,
+        adding event notifications to the "Current" loop would have no
+        effect at all, events would never fire.
+    */
+    wxASSERT_MSG( wxIsMainThread(),
+                    "sockets must be initialized from the main thread" );
+
+    wxAppConsole * const app = wxAppConsole::GetInstance();
+    wxCHECK_RET( app, "sockets can't be initialized without wxApp" );
+
+    ms_manager = app->GetTraits()->GetSocketManager();
+}