// Begins tracking a socket, calling Abort() if there was an earlier call to AbortAll() public void Add(IAsyncAbortableWebSocket webSocket) { int activeSocketCount; bool shouldAbort; // keep the lock for as short a period as possible lock (_activeSockets) { _activeSockets.Add(webSocket); activeSocketCount = _activeSockets.Count; shouldAbort = _aborted; } // perform any additional operations outside the lock _perfCounters.SetCounter(AppPerfCounter.REQUESTS_EXECUTING_WEBSOCKETS, activeSocketCount); if (shouldAbort) { webSocket.AbortAsync(); // don't care about the result of the abort at the present time } }