rewritten handshake, with guidance taken from the red5 source code