better handling of TCP read/write