Flushed the socket before it ends
Merge #447 r=@tcr: Flushed the socket before it ends

Fixes #445 and #422.