Code cleanup. Added internal connection state to prevent multiple reconnects