public static bool CheckHandshake(Context AContext)
 {
     if (AContext.ReceivedByteCount > 8)
     {
         ClientHandshake AHandshake = new ClientHandshake(new ArraySegment <byte>(AContext.Buffer, AContext.ReceivedByteCount - 8, 8), AContext.Header);
         // See if our header had the required information
         if (AHandshake.IsValid())
         {
             // Optionally check Origin and Location if they're set.
             if (Origin != string.Empty)
             {
                 if (AHandshake.Origin != "http://" + Origin)
                 {
                     return(false);
                 }
             }
             if (Location != string.Empty)
             {
                 if (AHandshake.Host != Location + ":" + AContext.Server.Port.ToString())
                 {
                     return(false);
                 }
             }
             // Generate response handshake for the client
             ServerHandshake ServerShake = GenerateResponseHandshake(AHandshake);
             // Send the response handshake
             SendServerHandshake(ServerShake, AContext);
             return(true);
         }
     }
     return(false);
 }
        private static void SendServerHandshake(ServerHandshake AHandshake, Context AContext)
        {
            // generate a byte array representation of the handshake including the answer to the challenge
            byte[] HandshakeBytes = AContext.UserContext.Encoding.GetBytes(AHandshake.ToString());
            Array.Copy(AHandshake.AnswerBytes, 0, HandshakeBytes, HandshakeBytes.Length - 16, 16);

            AContext.UserContext.SendRaw(HandshakeBytes);
        }
        private static ServerHandshake GenerateResponseHandshake(ClientHandshake AHandshake)
        {
            ServerHandshake AResponseHandshake = new ServerHandshake();

            AResponseHandshake.Location    = "ws://" + AHandshake.Host + AHandshake.ResourcePath;
            AResponseHandshake.Origin      = AHandshake.Origin;
            AResponseHandshake.SubProtocol = AHandshake.SubProtocol;
            AResponseHandshake.AnswerBytes = GenerateAnswerBytes(AHandshake.Key1, AHandshake.Key2, AHandshake.ChallengeBytes);

            return(AResponseHandshake);
        }
        private static ServerHandshake GenerateResponseHandshake(ClientHandshake AHandshake)
        {
            ServerHandshake AResponseHandshake = new ServerHandshake();
            AResponseHandshake.Location = "ws://" + AHandshake.Host + AHandshake.ResourcePath;
            AResponseHandshake.Origin = AHandshake.Origin;
            AResponseHandshake.SubProtocol = AHandshake.SubProtocol;
            AResponseHandshake.AnswerBytes = GenerateAnswerBytes(AHandshake.Key1, AHandshake.Key2, AHandshake.ChallengeBytes);

            return AResponseHandshake;
        }
        private static void SendServerHandshake(ServerHandshake AHandshake, Context AContext)
        {
            // generate a byte array representation of the handshake including the answer to the challenge
            byte[] HandshakeBytes = AContext.UserContext.Encoding.GetBytes(AHandshake.ToString());
            Array.Copy(AHandshake.AnswerBytes, 0, HandshakeBytes, HandshakeBytes.Length-16, 16);

            AContext.UserContext.SendRaw(HandshakeBytes);
        }