private void ProcessPingPong()
        {
            var contentLength = _pongBuffer.Count;

            if (CurrentHeader.ContentLength < 125)
            {
                contentLength = (int)CurrentHeader.ContentLength;
            }

            var readed = 0;

            while (CurrentHeader.RemainingBytes > 0)
            {
                readed = _clientStream.Read(_pongBuffer.Array, _pongBuffer.Offset + readed, contentLength - readed);
                CurrentHeader.DecodeBytes(_pongBuffer.Array, _pongBuffer.Offset, readed);
            }

            if (CurrentHeader.Flags.Option == WebSocketFrameOption.Pong)
            {
                _ping.NotifyPong(_pongBuffer);
            }
            else
            {// pong frames echo what was 'pinged'
                WriteInternal(_pongBuffer, readed, true, false, WebSocketFrameOption.Pong, WebSocketExtensionFlags.None);
            }
        }
        private void ProcessControlFrame(Stream clientStream)
        {
            switch (CurrentHeader.Flags.Option)
            {
            case WebSocketFrameOption.Continuation:
            case WebSocketFrameOption.Text:
            case WebSocketFrameOption.Binary:
                throw new WebSocketException("Text, Continuation or Binary are not protocol frames");

            case WebSocketFrameOption.ConnectionClose:
                this.Close(WebSocketCloseReasons.NormalClose);
                break;

            case WebSocketFrameOption.Ping:
            case WebSocketFrameOption.Pong:
                Int32 contentLength = _pongBuffer.Count;
                if (CurrentHeader.ContentLength < 125)
                {
                    contentLength = (Int32)CurrentHeader.ContentLength;
                }
                Int32 readed = 0;
                while (CurrentHeader.RemainingBytes > 0)
                {
                    readed = clientStream.Read(_pongBuffer.Array, _pongBuffer.Offset + readed, contentLength - readed);
                    CurrentHeader.DecodeBytes(_pongBuffer.Array, _pongBuffer.Offset, readed);
                }

                if (CurrentHeader.Flags.Option == WebSocketFrameOption.Pong)
                {
                    _ping.NotifyPong(_pongBuffer);
                }
                else     // pong frames echo what was 'pinged'
                {
                    this.WriteInternal(_pongBuffer, readed, true, false, WebSocketFrameOption.Pong, WebSocketExtensionFlags.None);
                }

                break;

            default: throw new WebSocketException("Unexpected header option '" + CurrentHeader.Flags.Option.ToString() + "'");
            }
        }