public async Task CloseAsync() { ThrowIfNotStarted(); Logger.LogInformation("Closing socket connection."); try { State = ReadyState.Closing; //don't receive anything from underlying socket anymore _pingPongSubscription.Dispose(); _messageSubscription.Dispose(); _disconnectSubscription.Dispose(); //clean anything set during socket startup/run time _packetId = -1; _currentHandshake = null; _sentPingPackets.Clear(); //finally stop the socket await _socket.StopOrFail(WebSocketCloseStatus.NormalClosure, string.Empty); } catch (Exception ex) { Logger.LogError(ex, "Error while stopping socket."); throw; } finally { Logger.LogInformation("Socket connection closed."); State = ReadyState.Closed; } }