void HandleCompletedAccept(IAsyncResult result) { IConnection connection = null; lock (ThisLock) { bool success = false; Exception unexpectedException = null; try { if (!isDisposed) { connection = listener.EndAccept(result); if (connection != null) { if (connections + 1 >= maxPendingConnections) { if (TD.MaxPendingConnectionsExceededIsEnabled()) { TD.MaxPendingConnectionsExceeded(SR.GetString(SR.TraceCodeMaxPendingConnectionsReached)); } if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.MaxPendingConnectionsReached, SR.GetString(SR.TraceCodeMaxPendingConnectionsReached), new StringTraceRecord("MaxPendingConnections", maxPendingConnections.ToString(System.Globalization.CultureInfo.InvariantCulture)), this, null); } } else if (TD.PendingConnectionsRatioIsEnabled()) { TD.PendingConnectionsRatio(connections + 1, maxPendingConnections); } // This is incremented after the Trace just in case the Trace throws. connections++; } } success = true; } catch (CommunicationException exception) { DiagnosticUtility.TraceHandledException(exception, TraceEventType.Information); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } if ((errorCallback == null) && !ExceptionHandler.HandleTransportExceptionHelper(exception)) { throw; } unexpectedException = exception; } finally { if (!success) { connection = null; } pendingAccepts--; if (pendingAccepts == 0 && TD.PendingAcceptsAtZeroIsEnabled()) { TD.PendingAcceptsAtZero(); } } if ((unexpectedException != null) && (errorCallback != null)) { errorCallback(unexpectedException); } } AcceptIfNecessary(false); if (connection != null) { callback(connection, onConnectionDequeued); } }