public static Handshake parse(ByteBuffer buffer)
		throws ParseException, UnsupportedEncodingException {
		
		if (buffer.remaining() < BASE_HANDSHAKE_LENGTH + 20) {
			throw new ParseException("Incorrect handshake message length.", 0);
		}
		
		int pstrlen = new Byte(buffer.get()).intValue();
		byte[] pstr = new byte[pstrlen==0x13 ? pstrlen : 0x13];
		//byte[] pstr = new byte[0x13];
		byte[] reserved = new byte[8];
		byte[] infoHash = new byte[20];
		byte[] peerID = new byte[20];
		boolean isObfuscated = false;
				
		buffer.get(pstr);
		buffer.get(reserved);
		buffer.get(infoHash);
		buffer.get(peerID);

		// if we have an ordinary bittorrent handshake
		if (BITTORRENT_PROTOCOL_IDENTIFIER.equals(new String(pstr, Torrent.BYTE_ENCODING))) {
			isObfuscated = false;
		} /* else we might have an obfuscated handshake */ 
		else if (Peer.isMagicPeerId(peerID)) {
			isObfuscated = true;
		}
		return new Handshake(buffer, ByteBuffer.wrap(infoHash), ByteBuffer.wrap(peerID), isObfuscated);
	}