Code to handle possible lost of connections