/// <summary> /// Begins an operation to accept a connection from a client /// </summary> private void StartAccept() { SocketAsyncEventArgs acceptEventArg; if (_acceptPool.Count > 1) { try { acceptEventArg = _acceptPool.Pop(); } catch { acceptEventArg = CreateAcceptingSocketAsyncEventArgs(); } } else { acceptEventArg = CreateAcceptingSocketAsyncEventArgs(); } // This is a mechanism to prevent exceeding // the max # of connections we specified. We'll do this before // doing AcceptAsync. If maxConnections value has been reached, // then the application will pause here until the Semaphore gets released, // which happens in the CloseClientSocket method. _maxConnectionsEnforcer.WaitOne(); try { bool runningAsync = _listenSocket.AcceptAsync(acceptEventArg); if (!runningAsync) { // Accept completed synchronously (Completed callback was not called). ProcessAccept(acceptEventArg); } } catch (ObjectDisposedException err) { // Handle silently, the program is most likely being terminated. // Trace just in case we get here in an unexpected way. _trace.DisposedListen(_settings.CustomState, err); } }