Added code to handle connection failure