Refactored connection_close function.