/// <summary>
 /// Handles the initial request.
 /// Attempts to process the header that should have been sent.
 /// Otherwise, through magic and wizardry, the client gets disconnected.
 /// </summary>
 /// <param name="context">The user context.</param>
 public override void HandleRequest(Context context)
 {
     if (context.IsSetup)
     {
         context.Dispose();
     }
     else
     {
         ProcessHeader(context);
     }
 }
        /// <summary>
        /// Processes the header.
        /// </summary>
        /// <param name="context">The user context.</param>
        public void ProcessHeader(Context context)
        {
            string data = context.UserContext.Encoding.GetString(context.Buffer, 0, context.ReceivedByteCount);

            //Check first to see if this is a flash socket XML request.
            if (data == "<policy-file-request/>\0")
            {
                try
                {
                    //if it is, we access the Access Policy Server instance to send the appropriate response.
                    context.Server.AccessPolicyServer.SendResponse(context.Connection);
                }
                catch {}

                context.Dispose();
            }
            else//If it isn't, process http/websocket header as normal.
            {
                context.Header = new Header(data);

                switch (context.Header.Protocol)
                {
                    case Protocol.WebSocket:
                        context.Handler = WebSocketHandler.Instance;

                        break;
                    case Protocol.FlashSocket:
                        context.Handler = WebSocketHandler.Instance;

                        break;
                    default:
                        context.Header.Protocol = Protocol.None;

                        break;
                }

                if (context.Header.Protocol != Protocol.None)
                {
                    context.Handler.HandleRequest(context);
                }
                else
                {
                    context.UserContext.Send(Response.NotImplemented, true);
                }
            }
        }
 /// <summary>
 /// Attempts to authenticates the specified user context.
 /// If authentication fails it kills the connection.
 /// </summary>
 /// <param name="context">The user context.</param>
 private void Authenticate(Context context)
 {
     if (WebSocketAuthentication.CheckHandshake(context))
     {
         context.UserContext.Protocol = context.Header.Protocol;
         context.UserContext.RequestPath = context.Header.RequestPath;
         context.Header = null;
         context.IsSetup = true;
     }
     else
     {
         context.Dispose();
     }
 }