Refactoring connection_close function.