refactor connection establishment

reduces duplicated code, and makes it easier for future changes
to connection establishment because it's all in one place now
We need to pull `@result` out of this method. It would be fine to  have a local `result` that gets passed back to the caller.

`0` is a strange default, too.