Tweak connection flow and add keep-alive support

The keep-alive support is not complete and needs further tweaking.