Fix data writing to stream

There were problem with streaming data, when using embed images or while sending big message.
It was due to the fact that fwrite operations on non-blocking socket streams can be interrupted by the arrival of new packets.
After this fix writing continues  until all data is sent.