Fix critical bug in socket handshake