Added automatic reconnect functionality