Added missing connection exception handling and termination code