Refactor internal connection-handling to simplify logic.