In theory this writing code works, in practice not so much

The server is basically sending the data too quickly. In all of my test
cases after I read the 'header' it hadn't received enough to fill the
buffer. In practice this kind of means that it will work with small files,
with bigger files it will just work for the first few bytes, after that
it fucks up.