/// <summary>
        /// Disconnects the socket asynchronously.
        /// </summary>
        /// <returns>Returns a Task that can be used to wait for the operation to complete.</returns>
        public async Task DisconnectAsync()
        {
            CancellationTokenSource tsource = new CancellationTokenSource(20000);

            await this.socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "closing", tsource.Token);

            connectionManager.Cancel();
            OnSocketDisconnected?.Invoke(this);
        }
        /// <summary>
        /// Disconnects the socket blocking until the operation completes.
        /// </summary>
        public void Disconnect()
        {
            CancellationTokenSource tsource = new CancellationTokenSource(20000);
            Task toAwait = this.socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "closing", tsource.Token);

            Task.WaitAll(toAwait);
            connectionManager.Cancel();
            OnSocketDisconnected?.Invoke(this);
        }
 /// <summary>
 /// Use this function to disconnect the socket asynchronously. Once the operation succeeds, the provided callback will be invoked.
 /// </summary>
 /// <param name="callback">Callback invoked when the socket is disconnected.</param>
 public void DisconnectAsync(Action callback)
 {
     Task.Run(async() =>
     {
         CancellationTokenSource tsource = new CancellationTokenSource(20000);
         await this.socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "closing", tsource.Token);
         OnSocketDisconnected?.Invoke(this);
         connectionManager.Cancel();
         callback?.Invoke();
     });
 }
 /// <summary>
 /// Use this function to disconnect the socket asynchronously. Once the operation succeeds, the provided callback will be invoked.
 /// </summary>
 /// <param name="callback">Callback invoked when the socket is disconnected.</param>
 public void DisconnectAsync(Action callback)
 {
     Connected = false;
     Task.Run(() =>
     {
         socket.Shutdown(SocketShutdown.Both);
         socket.Close();
         OnSocketDisconnected?.Invoke(this);
         callback?.Invoke();
     });
 }
        /// <summary>
        /// Disconnects the socket blocking until the operation completes.
        /// </summary>
        public void Disconnect()
        {
            Connected = false;
            var _socket = socket;

            socket = null;
            _socket?.Shutdown(SocketShutdown.Both);
            _socket?.Close();
            _socket?.Dispose();


            OnSocketDisconnected?.Invoke(this);
        }
 /// <summary>
 /// Disconnects the socket asynchronously.
 /// </summary>
 /// <returns>Returns a Task that can be used to wait for the operation to complete.</returns>
 public async Task DisconnectAsync()
 {
     Connected = false;
     await Task.Run(() =>
     {
         var _socket = socket;
         socket      = null;
         _socket?.Shutdown(SocketShutdown.Both);
         _socket?.Close();
         _socket?.Dispose();
         OnSocketDisconnected?.Invoke(this);
         OnSocketDisconnected?.Invoke(this);
     });
 }
 /// <summary>
 /// Use this function to disconnect the socket asynchronously. Once the operation succeeds, the provided callback will be invoked.
 /// </summary>
 /// <param name="callback">Callback invoked when the socket is disconnected.</param>
 public void DisconnectAsync(Action callback)
 {
     socket.Shutdown(SocketShutdown.Both);
     socket.BeginDisconnect(false, (e) =>
     {
         try
         {
             socket.EndDisconnect(e);
             socket.Shutdown(SocketShutdown.Both);
             socket.Close();
             callback?.Invoke();
         }
         catch (Exception ex)
         {
             throw ex;
         }
         finally
         {
             OnSocketDisconnected?.Invoke(this);
         }
     }, this);
 }
        /// <summary>
        /// Disconnects the socket asynchronously.
        /// </summary>
        /// <returns>Returns a Task that can be used to wait for the operation to complete.</returns>
        public async Task DisconnectAsync()
        {
            TaskCompletionSource <bool> task = new TaskCompletionSource <bool>();

            socket.Shutdown(SocketShutdown.Both);
            socket.BeginDisconnect(false, (e) =>
            {
                try
                {
                    socket.EndDisconnect(e);
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                    task.TrySetResult(true);
                }
                catch (Exception ex) {
                    task.SetException(ex);
                }
                finally
                {
                    OnSocketDisconnected?.Invoke(this);
                }
            }, this);
            await task.Task;
        }