added support for automatic reconnection