protected virtual Task CloseAsync() { if (!Connected) { return(Task.FromResult(false)); } Interlocked.CompareExchange(ref _connected, 0, 1); _cts.Cancel(); _packetQueue.CompleteAdding(); try { if (ConnectionClient != null) { ConnectionClient.Disconnect(false); if (ConnectionClient.Poll(10000, SelectMode.SelectRead)) { } } } catch { } finally { EventHandler disconnected = Disconnected; if (disconnected != null) { disconnected(this, EventArgs.Empty); } if (ConnectionClient != null) { ConnectionClient.Close(); ConnectionClient = null; } if (Completed != null) { Completed.SetResult(true); } } return(Task.FromResult(false)); }