Added code to automatically re-establish lost connection