+#ifdef WIN32
+
+typedef struct _ThreadList ThreadList;
+
+struct _ThreadList
+{
+ HANDLE hThread;
+ IOCHAN pIOChannel;
+ ThreadList *pNext;
+};
+
+static ThreadList *pFirstThread;
+static CRITICAL_SECTION Thread_CritSect;
+static BOOL bInitialized = FALSE;
+
+static void ThreadList_Initialize()
+{
+ /* Initialize the critical Sections */
+ InitializeCriticalSection(&Thread_CritSect);
+
+ /* Set the first thraed */
+ pFirstThread = NULL;
+
+ /* we have been initialized */
+ bInitialized = TRUE;
+}
+
+static void statserv_add(HANDLE hThread, IOCHAN pIOChannel)
+{
+ /* Only one thread can go through this section at a time */
+ EnterCriticalSection(&Thread_CritSect);
+
+ {
+ /* Lets create our new object */
+ ThreadList *pNewThread = (ThreadList *)malloc(sizeof(ThreadList));
+ pNewThread->hThread = hThread;
+ pNewThread->pIOChannel = pIOChannel;
+ pNewThread->pNext = pFirstThread;
+ pFirstThread = pNewThread;
+
+ /* Lets let somebody else create a new object now */
+ LeaveCriticalSection(&Thread_CritSect);
+ }
+}
+
+void statserv_remove(IOCHAN pIOChannel)
+{
+ /* Only one thread can go through this section at a time */
+ EnterCriticalSection(&Thread_CritSect);
+
+ {
+ ThreadList *pCurrentThread = pFirstThread;
+ ThreadList *pNextThread;
+ ThreadList *pPrevThread =NULL;
+
+ /* Step through alll the threads */
+ for (; pCurrentThread != NULL; pCurrentThread = pNextThread)
+ {
+ /* We only need to compare on the IO Channel */
+ if (pCurrentThread->pIOChannel == pIOChannel)
+ {
+ /* We have found the thread we want to delete */
+ /* First of all reset the next pointers */
+ if (pPrevThread == NULL)
+ pFirstThread = pCurrentThread->pNext;
+ else
+ pPrevThread->pNext = pCurrentThread->pNext;
+
+ /* All we need todo now is delete the memory */
+ free(pCurrentThread);
+
+ /* No need to look at any more threads */
+ pNextThread = NULL;
+ }
+ else
+ {
+ /* We need to look at another thread */
+ pNextThread = pCurrentThread->pNext;
+ pPrevThread = pCurrentThread;
+ }
+ }
+
+ /* Lets let somebody else remove an object now */
+ LeaveCriticalSection(&Thread_CritSect);
+ }
+}
+
+/* WIN32 statserv_closedown */
+void statserv_closedown()
+{
+ /* Shouldn't do anything if we are not initialized */
+ if (bInitialized)
+ {
+ int iHandles = 0;
+ HANDLE *pThreadHandles = NULL;
+
+ /* We need to stop threads adding and removing while we */
+ /* start the closedown process */
+ EnterCriticalSection(&Thread_CritSect);
+
+ {
+ /* We have exclusive access to the thread stuff now */
+ /* Y didn't i use a semaphore - Oh well never mind */
+ ThreadList *pCurrentThread = pFirstThread;
+
+ /* Before we do anything else, we need to shutdown the listener */
+ if (pListener != NULL)
+ iochan_destroy(pListener);
+
+ for (; pCurrentThread != NULL; pCurrentThread = pCurrentThread->pNext)
+ {
+ /* Just destroy the IOCHAN, that should do the trick */
+ iochan_destroy(pCurrentThread->pIOChannel);
+ closesocket(pCurrentThread->pIOChannel->fd);
+
+ /* Keep a running count of our handles */
+ iHandles++;
+ }
+
+ if (iHandles > 0)
+ {
+ HANDLE *pCurrentHandle ;
+
+ /* Allocate the thread handle array */
+ pThreadHandles = (HANDLE *)malloc(sizeof(HANDLE) * iHandles);
+ pCurrentHandle = pThreadHandles;
+
+ for (pCurrentThread = pFirstThread;
+ pCurrentThread != NULL;
+ pCurrentThread = pCurrentThread->pNext, pCurrentHandle++)
+ {
+ /* Just the handle */
+ *pCurrentHandle = pCurrentThread->hThread;
+ }
+ }
+
+ /* We can now leave the critical section */
+ LeaveCriticalSection(&Thread_CritSect);
+ }
+
+ /* Now we can really do something */
+ if (iHandles > 0)
+ {
+ logf (LOG_LOG, "waiting for %d to die", iHandles);
+ /* This will now wait, until all the threads close */
+ WaitForMultipleObjects(iHandles, pThreadHandles, TRUE, INFINITE);
+
+ /* Free the memory we allocated for the handle array */
+ free(pThreadHandles);
+ }
+
+ if (control_block.bend_stop)
+ (*control_block.bend_stop)(&control_block);
+ /* No longer require the critical section, since all threads are dead */
+ DeleteCriticalSection(&Thread_CritSect);
+ }
+}
+
+void __cdecl event_loop_thread (IOCHAN iochan)
+{
+ event_loop (&iochan);
+}
+
+/* WIN32 listener */
+static void listener(IOCHAN h, int event)