You could also try this. In theory, read() does not use a buffer, so there might be data loss there as well. This is my last idea. With my code it works, even with a large codebase. Of course, in some cases this might make things worse. In any case, if you can, give this a try as well.
#include...