Implement timeout and automatic reconnect