private static void SendAndReceiveChunksAndRebuildFile(Stream stream, List<Chunk> chunks, Stream fileStreamNew, Stream fileStreamOld, int chunksToRequest, byte[] buffer) { chunksToRequest.WriteTo(stream); chunks.Sort((i1, i2) => i1.NewStart.CompareTo(i2.NewStart)); foreach (var chunk in chunks) { if (chunk.Found) // From old file { fileStreamOld.Seek(chunk.OldStart, 0); fileStreamNew.Seek(chunk.NewStart, 0); fileStreamOld.ReadWrite(fileStreamNew, chunk.Length, buffer); } else // From remote file { chunk.NewStart.WriteTo(stream); chunk.Length.WriteTo(stream); fileStreamNew.Seek(chunk.NewStart, 0); stream.ReadWrite(fileStreamNew, chunk.Length, buffer); } } }