internal static Stream ExecuteCommands <TFormat>(
            IProcessingEngine engine,
            IWireSerialization serialization,
            IServerCommandDescription <TFormat>[] commands,
            IRequestContext request,
            IResponseContext response,
            string accept)
        {
            var result = Execute(engine, commands, request, response);

            if (result.Error != null)
            {
                return(result.Error);
            }
            if (result.Result == null)
            {
                response.ContentType = accept;
                return(null);
            }
            if (accept == "application/octet-stream")
            {
                response.ContentType = "application/octet-stream";
                if (result.Result is Stream)
                {
                    return(result.Result as Stream);
                }
                else if (result.Result is StreamReader)
                {
                    return((result.Result as StreamReader).BaseStream);
                }
                else if (result.Result is StringBuilder)
                {
                    var sb  = result.Result as StringBuilder;
                    var cms = ChunkedMemoryStream.Create();
                    var sw  = cms.GetWriter();
                    for (int i = 0; i < sb.Length;)
                    {
                        var min = Math.Min(sb.Length - i, cms.CharBuffer.Length);
                        sb.CopyTo(i, cms.CharBuffer, 0, min);
                        i += min;
                        sw.Write(cms.CharBuffer, 0, min);
                    }
                    sw.Flush();
                    cms.Position = 0;
                    return(cms);
                }
                else if (result.Result is byte[])
                {
                    return(new MemoryStream(result.Result as byte[]));
                }
                else if (result.Result is string)
                {
                    var cms = ChunkedMemoryStream.Create();
                    var sw  = cms.GetWriter();
                    sw.Write(result.Result as string);
                    sw.Flush();
                    cms.Position = 0;
                    return(cms);
                }
                else if (result.Result is char[])
                {
                    var cms = ChunkedMemoryStream.Create();
                    var sw  = cms.GetWriter();
                    sw.Write(result.Result as char[]);
                    sw.Flush();
                    cms.Position = 0;
                    return(cms);
                }
                return(response.ReturnError(
                           "Unexpected command result. Can't convert "
                           + result.Result.GetType().FullName + " to octet-stream. Use application/x-dotnet mime type for .NET binary serialization",
                           HttpStatusCode.UnsupportedMediaType));
            }
            if (accept == "application/base64")
            {
                response.ContentType = "application/base64";
                if (result.Result is Stream)
                {
                    var stream = result.Result as Stream;
                    try
                    {
                        var cms = stream as ChunkedMemoryStream;
                        if (cms != null)
                        {
                            return(cms.ToBase64Stream());
                        }
                        else
                        {
                            cms = new ChunkedMemoryStream(stream);
                            try { return(cms.ToBase64Stream()); }
                            finally { cms.Dispose(); }
                        }
                    }
                    finally { stream.Dispose(); }
                }
                else if (result.Result is StreamReader)
                {
                    var sr = result.Result as StreamReader;
                    try
                    {
                        var cms = sr.BaseStream as ChunkedMemoryStream;
                        if (cms != null)
                        {
                            return(cms.ToBase64Stream());
                        }
                        else
                        {
                            cms = new ChunkedMemoryStream(sr.BaseStream);
                            try { return(cms.ToBase64Stream()); }
                            finally { cms.Dispose(); }
                        }
                    }
                    finally { sr.Dispose(); }
                }
                else if (result.Result is StringBuilder)
                {
                    var sb = result.Result as StringBuilder;
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        var sw = cms.GetWriter();
                        for (int i = 0; i < sb.Length;)
                        {
                            var min = Math.Min(sb.Length - i, cms.CharBuffer.Length);
                            sb.CopyTo(i, cms.CharBuffer, 0, min);
                            i += min;
                            sw.Write(cms.CharBuffer, 0, min);
                        }
                        sw.Flush();
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                else if (result.Result is byte[])
                {
                    var bytes = result.Result as byte[];
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        cms.Write(bytes, 0, bytes.Length);
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                else if (result.Result is string)
                {
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        var sw = cms.GetWriter();
                        sw.Write(result.Result as string);
                        sw.Flush();
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                else if (result.Result is char[])
                {
                    using (var cms = ChunkedMemoryStream.Create())
                    {
                        var sw = cms.GetWriter();
                        sw.Write(result.Result as char[]);
                        sw.Flush();
                        cms.Position = 0;
                        return(cms.ToBase64Stream());
                    }
                }
                return(response.ReturnError("Unexpected command result. Can't convert to base64.", HttpStatusCode.UnsupportedMediaType));
            }
            if (accept == "application/x-dotnet")
            {
                response.ContentType = "application/x-dotnet";
                var bf = new BinaryFormatter();
                bf.AssemblyFormat = FormatterAssemblyStyle.Simple;
                var cms = ChunkedMemoryStream.Create();
                bf.Serialize(cms, result.Result);
                cms.Position = 0;
                return(cms);
            }
            var ms = ChunkedMemoryStream.Create();

            response.ContentType = serialization.Serialize(result.Result, accept, ms);
            ms.Position          = 0;
            return(ms);
        }