Various fixes and refactorings to ensure that the incoming stream always gets read completely (we missed some corner cases in a previous attempt to fix this).