I don't understand your problem,but let's assume that we have those streams:
in:the input stream from server
out: the output stream to server
outfile: the output stream to file.
I suppose that...