refactor connection establishment

reduces duplicated code, and makes it easier for future changes
to connection establishment because it's all in one place now
Was thinking it'd be good to document that the result of the block is passed through, since that's relevant to the `@result =` assignment we do in the caller. Not a blocker but would be good to communicate that.