private void handshake( BufferedReader in ) throws IOException, NoSuchAlgorithmException {

      MessageDigest sha1 = MessageDigest.getInstance("SHA1");              // our sha-1, for hashing the input key       
      PrintWriter out = new PrintWriter(client.getOutputStream(), true );  // output to the socket

      char[] cStr = new char[4096];
      in.read(cStr);
      String header = new String(cStr);
      
      if ( DEBUG ) { System.out.println( "From handshake:\n" + header ); }
      
      String key = header.split("Sec-WebSocket-Key: ")[1].split( "\r\n" )[0].trim();      
      
      if ( DEBUG ) { System.out.println(key); }
      
      key = key.concat("258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); // adds magic string (globally unique identifier)
            
      // Hash and 64-bit encode our key to get our reponse
      byte[] bytes = key.getBytes(("UTF-8"));
      sha1.update(bytes);
      byte[] digest = sha1.digest();
      String hash = (new BASE64Encoder()).encode(digest);
      
      if ( DEBUG ) { System.out.println( hash ); }
           
      // build our reply to the client
      String reply = "HTTP/1.1 101 Switching Protocols\r\n" +
                     "Upgrade: websocket\r\n" +
                     "Connection: Upgrade\r\n" +
                     "Sec-WebSocket-Accept: " + hash + "\r\n\r\n";
      
      if ( DEBUG ) { System.out.println("*****Reply**********\n"+reply); } 
      
      // send our reply to complete the handshake!
      out.print(reply);
      out.flush();
   }