/// <summary> /// Closes the connection and cleans all the values related to the object. /// </summary> public async void Dispose() { if ((CancellationTokenSource != null) && (CancellationTokenSource.IsCancellationRequested)) { CancellationTokenSource.Cancel(); } if (InboundStream != null) { InboundStream.Dispose(); } if (OutboundStream != null) { OutboundStream.Dispose(); } if (StreamReader != null) { StreamReader.Dispose(); } if (StreamWriter != null) { StreamWriter.Dispose(); } if (Listener != null) { await Listener.CancelIOAsync(); Listener.ConnectionReceived -= Listener_ConnectionReceived; Listener.ConnectionReceived -= ConnectionReceived; Listener.Dispose(); } Listener = null; Port = -1; ErrorStatus = null; CancellationTokenSource = null; }