public async void Client_RequestedStreamRead(object sender, NetworkSocket.RequestStreamReadArgs args) { NetworkSocket client = sender as NetworkSocket; byte[] buff = new byte[args.ReadLen]; int bytesRead = 0; //We need to determine if localhost is the host of the files, otherwise we need to forward the request //onto the host of the files if (DoesTokenExist(args.Token)) { try { bytesRead = await ReadStream(args.Token, args.File, buff, 0, args.ReadLen); } catch (Exception ex) { ISLogger.Write("Failed to read stream: " + ex.Message); ISLogger.Write(ex.StackTrace); client.SendFileErrorResponse(args.NetworkMessageId, "An error occurred while reading from stream: " + ex.Message); return; } } if (bytesRead != buff.Length) { //resize the buffer so we don't send a buffer that ends with empty data. byte[] resizedBuffer = new byte[bytesRead]; Buffer.BlockCopy(buff, 0, resizedBuffer, 0, bytesRead); buff = resizedBuffer; } client.SendReadRequestResponse(args.NetworkMessageId, buff); }