added auto-reconnect functionality