/// <summary> /// Callback method for accepting new connections /// </summary> private void AcceptCallback(IAsyncResult ar) { if (listeningThread.ManagedThreadId == Thread.CurrentThread.ManagedThreadId) { new Thread(() => AcceptCallback(ar)).Start(); return; } if (IsShuttingDown) { return; } // Get the socket that handles the client request var listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); // Signal the main thread to continue listenThreadSwitch.Set(); // Create the state object var state = new SocketState(handler); state.guid = Guid.NewGuid(); lock (ConnectedSockets) { ConnectedSockets[state.guid] = state; } QTransfer transfer = null; try { transfer = new QTransfer(state); transfer.OnReceiveRequest = OnReceiveRequest; transfer.OnReceiveResponse = OnReceiveResponse; } catch (Exception e) { Console.WriteLine(e.Message); } if (transfer == null) { CloseSocket(state); return; } // No need for asynchronous I/O from now on try { while (transfer.LogicLoop()) { if (IsShuttingDown || state.IsSocketDead()) { break; } } } catch (SocketException) { /* ignore */ } catch (IoBroken) { /* ignore */ } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Closing socket on error"); } CloseSocket(state); }