public bool CheckHandshake(Context context)
 {
     if (context.ReceivedByteCount > 8)
     {
         ClientHandshake handshake = new ClientHandshake(new ArraySegment<byte>(context.Buffer, context.ReceivedByteCount - 8, 8), context.Header);
         // See if our header had the required information
         if (handshake.IsValid())
         {
             // Optionally check Origin and Location if they're set.
             if (Origin != string.Empty)
                 if (handshake.Origin != "http://" + Origin)
                     return false;
             if (Location != string.Empty)
                 if (handshake.Host != Location + ":" + context.Server.Port.ToString())
                     return false;
             // Generate response handshake for the client
             ServerHandshake serverShake = GenerateResponseHandshake(handshake);
             // Send the response handshake
             SendServerHandshake(serverShake, context);
             return true;
         }
     }
     return false;
 }
        private static ServerHandshake GenerateResponseHandshake(ClientHandshake handshake)
        {
            ServerHandshake responseHandshake = new ServerHandshake();
            responseHandshake.Location = "ws://" + handshake.Host + handshake.ResourcePath;
            responseHandshake.Origin = handshake.Origin;
            responseHandshake.SubProtocol = handshake.SubProtocol;
            responseHandshake.AnswerBytes = GenerateAnswerBytes(handshake.Key1, handshake.Key2, handshake.ChallengeBytes);

            return responseHandshake;
        }
        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;
        }