public void Close() { lock (_InitializationLock) { try { if (null != _Socket) { _Socket.Close(); _Socket = null; } if (null != _IncomingStreamHandler) { _IncomingStreamHandler.Stop(); _IncomingStreamHandler = null; } if (null != _OutgoingStreamHandler) { _OutgoingStreamHandler.Stop(); _OutgoingStreamHandler = null; } //notify connection change if (null != _ConnectionListener) { _ConnectionListener.OnGazeApiConnectionStateChanged(false); } //We cancel queued requests if (null != _RequestQueue) { CancelAllRequests(); _RequestQueue.Stop(); } _RequestQueue = null; //We make sure we cancel currently ongoing requests if (null != _OngoingRequests) { IEnumerator <KeyValuePair <int, IRequest> > reqs = _OngoingRequests.GetEnumerator(); while (reqs.MoveNext()) { reqs.Current.Value.Cancel(); } _OngoingRequests.Clear(); } _OngoingRequests = null; } catch (Exception e) { Debug.WriteLine("Error closing socket: " + e.Message); } } }