Add debug option. Improve connection handshake handling.