Refactored connection_open function.