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.
I know this seems a detail, but I think the character ü was missed here. :) I also noticed some other missing chars in the japanese files too.