in reply to Increase IO buffer

Use something similar to chunked transfer encoding which would allow you to detect the end-of-message easyly (it is marked by a 0-length chunk).