Refactor connection state management (#326)

This PR does the work described in #326. All unit tests pass.

I've split the work into 3 commits, and tried to make each as clear and self-contained as possible, to help reviews.

I'd really appreciate if you guys could take a look, this touches on some very fundamental things.