The canonical way to detect EOF on read is when you request more than zero bytes, but zero bytes come back. Switching to this method seems to handle a few problem cases better. Also handle ERROR_BROKEN_PIPE properly just in case.