+// --------------------------------------------------------------------------
+// Initialization and shutdown
+// --------------------------------------------------------------------------
+
+// FIXME-MT: all this is MT-unsafe, of course, we should protect all accesses
+//           to m_countInit with a crit section
+size_t wxSocketBase::m_countInit = 0;
+
+bool wxSocketBase::IsInitialized()
+{
+    return m_countInit > 0;
+}
+
+bool wxSocketBase::Initialize()
+{
+    if ( !m_countInit++ )
+    {
+        /*
+            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(),
+            wxT("Call wxSocketBase::Initialize() from the main thread first!"));
+
+        wxAppTraits *traits = wxAppConsole::GetInstance() ?
+                              wxAppConsole::GetInstance()->GetTraits() : NULL;
+        GSocketGUIFunctionsTable *functions =
+            traits ? traits->GetSocketGUIFunctionsTable() : NULL;
+        GSocket_SetGUIFunctions(functions);
+
+        if ( !GSocket_Init() )
+        {
+            m_countInit--;
+
+            return false;
+        }
+    }
+
+    return true;
+}
+
+void wxSocketBase::Shutdown()
+{
+    // we should be initialized
+    wxASSERT_MSG( m_countInit, _T("extra call to Shutdown()") );
+    if ( !--m_countInit )
+    {
+        GSocket_Cleanup();
+    }
+}
+