public void Close()
        {
            try
            {
                if (BufferedWrite)
                {
                    Send(WebSocketMessage.Create(new byte[0], Opcode.Close, MaskOutgoing));
                }
                else
                {
                    SendReal(WebSocketMessage.Create(new byte[0], Opcode.Close, MaskOutgoing).Serialize());
                }
            }
            catch
            {
            }
            Closed = true;
            Client.Close();

            // unblock external Receive() calls
            ReceiveQueue.Enqueue(new byte[0]);
        }
        private void ReceiveLoop()
        {
            WebSocketMessage message = new WebSocketMessage();

            while (Client.Connected && !Closed)
            {
                try
                {
                    var result = WebSocketMessage.FromStream(message, NetworkStream);
                    if (result == null)
                    {
                        Close();
                        return;
                    }

                    if (message.Final)
                    {
                        if (FragmentStart != null)
                        {
                            if (message.Opcode != Opcode.Continuation)
                            {
                                // control message
                            }
                            else
                            {
                                FragmentBuffer.Write(message.Payload, 0, message.Payload.Length);

                                if (OnDataReceived != null)
                                {
                                    OnDataReceived(this, FragmentStart, FragmentBuffer.ToArray());
                                }

                                //ReceiveQueue.Enqueue(FragmentBuffer.ToArray());

                                FragmentStart = null;
                                FragmentBuffer.SetLength(0);
                            }
                        }
                        else
                        {
                            switch (message.Opcode)
                            {
                            case Opcode.Binary:
                            case Opcode.Text:
                                //ReceiveQueue.Enqueue(message.Payload);

                                if (OnDataReceived != null)
                                {
                                    OnDataReceived(this, message, message.Payload);
                                }

                                break;

                            case Opcode.Ping:
                                Send(WebSocketMessage.Create(message.Payload, Opcode.Pong, MaskOutgoing));
                                Log.Info("Received ping");
                                break;

                            case Opcode.Pong:
                                Log.Info("Received pong");
                                break;

                            case Opcode.Close:
                                Send(WebSocketMessage.Create(new byte[0], Opcode.Close, MaskOutgoing));
                                Close();
                                Log.Info("Received WebSocket close, disconnected");
                                return;
                            }
                        }
                    }
                    else
                    {
                        if (FragmentStart == null)
                        {
                            // start receiving a fragment
                            FragmentStart = message;
                            FragmentBuffer.SetLength(0);
                            FragmentBuffer.Write(message.Payload, 0, message.Payload.Length);
                        }
                        else
                        {
                            if (message.Opcode != Opcode.Continuation)
                            {
                                // illegal
                            }
                            else
                            {
                                FragmentBuffer.Write(message.Payload, 0, message.Payload.Length);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    Close();
                }
            }
        }
 public void SendText(string data)
 {
     Send(WebSocketMessage.Create(Encoding.UTF8.GetBytes(data), Opcode.Text, MaskOutgoing));
 }
 public void SendPing()
 {
     Send(WebSocketMessage.Create(new byte[0], Opcode.Ping, MaskOutgoing));
 }