connection: made code more linear

make it easier to read and grok