Take advantage of StreamReaderProtocol.

This is a step towards supporting flow control.

Refactor connection termination to better follow the specification:
close the TCP connection properly with shutdown() then close().