public byte[] ReadFully(Stream stream) { Packager pkg = new Packager(); ProtocolStack ps = new ProtocolStack(); ps.ip = ip; ps.receive_port = receive_port; ps.send_port = send_port; byte[] buffer = new byte[1024]; //set the size of your buffer (chunk) using (MemoryStream ms = new MemoryStream()) //You need a db connection instead { while (true) //loop to the end of the file { int read = stream.Read(buffer, 0, buffer.Length); //read each chunk if (read <= 0) { return(ms.ToArray()); } ms.Write(buffer, 0, read); //write chunk to [wherever] ms.Flush(); while (true) { Thread.Sleep(500); ps.sendData(pkg.pack(buffer)); string buff = ps.receiveData()[0]; if (buff.Equals("1")) { break; } } } } ps.sendData(pkg.pack(Encoding.ASCII.GetBytes("EOF"))); }