private void OnReceived(object sender, SocketAsyncEventArgs eventArgs) { while (true) { if (TcpHelper.ShouldHandleError(eventArgs, out bool isRemoteShutdown)) { if (isRemoteShutdown) { UserToken userToken = (UserToken)eventArgs.UserToken; if (!ForcedDisconnect(userToken.Socket)) { return; } _connectionLostHandler(userToken.Socket); userToken.Deinitialize(); lock (_receiveEventArgsQueue) { _receiveEventArgsQueue.Enqueue(eventArgs); } } return; } UserToken token = (UserToken)eventArgs.UserToken; if (token.ShouldHandle()) { _receiveHandler(token.Socket, eventArgs.Buffer, eventArgs.BytesTransferred); } try { if (token.Socket.ReceiveAsync(eventArgs)) { break; } } catch (Exception e) when(e is ObjectDisposedException || e is InvalidOperationException) { return; } } }