MainStream.Read(streambuffer,MainStream.Size);
You read in the complete MainStream.Size here into the Streambuffer.
So there is no need for a loop because you read the entire stream in one go.
Do note that you'r buffer needs to be big enough for the complete file.
But you have a string as buffer. My first question is... what kind of files are you transfering.
If they are binary you'll need to use an array of bytes as buffer, not a string type.
If they are binary, you can forget about buffers and just do a
Connection.SendStreamRaw(Stream);
and
Connection.RecvStreamRaw(Stream);
without any loop.
Using the SendStreamRaw and RecvStreamRaw, Synapse will internally take care of everything.
MainStream.Create(infile_name,fmCreate);
Connection.RecvStreamRaw(MainStream, 60000); // 60 seconds timeout
MainStream.Free;
MainStream.Create(OpenFileDialog.FileName,fmOpenRead);
Connection.SendStreamRaw(MainStream);
MainStream.Free;