internal static Task Write(ConnectionContext connection, GrainId grainId, SiloAddress siloAddress)
        {
            var output       = connection.Transport.Output;
            var outputWriter = new PrefixingBufferWriter <byte, PipeWriter>(output, sizeof(int), 1024, MemoryPool <byte> .Shared);
            var writer       = new BinaryTokenStreamWriter2 <PrefixingBufferWriter <byte, PipeWriter> >(outputWriter);

            writer.Write(grainId);
            if (!(siloAddress is null))
            {
                writer.Write(siloAddress);
            }
            writer.Commit();

            var length = outputWriter.CommittedBytes;

            if (length > MaxPreambleLength)
            {
                throw new InvalidOperationException($"Created preamble of length {length}, which is greater than maximum allowed size of {MaxPreambleLength}.");
            }

            Span <byte> lengthSpan = stackalloc byte[4];

            BinaryPrimitives.WriteInt32LittleEndian(lengthSpan, length);
            outputWriter.Complete(lengthSpan);

            var flushTask = output.FlushAsync();

            if (flushTask.IsCompletedSuccessfully)
            {
                return(Task.CompletedTask);
            }
            return(flushTask.AsTask());
        }
        internal static async System.Threading.Tasks.ValueTask Write(ConnectionContext connection, GrainId nodeIdentity, NetworkProtocolVersion protocolVersion, SiloAddress siloAddress)
        {
            var output       = connection.Transport.Output;
            var outputWriter = new PrefixingBufferWriter <byte, PipeWriter>(sizeof(int), 1024, MemoryPool <byte> .Shared);

            outputWriter.Reset(output);
            var writer = new BinaryTokenStreamWriter2 <PrefixingBufferWriter <byte, PipeWriter> >(outputWriter);

            writer.Write(nodeIdentity);
            writer.Write((byte)protocolVersion);

            if (siloAddress is null)
            {
                writer.WriteNull();
            }
            else
            {
                writer.Write((byte)SerializationTokenType.SiloAddress);
                writer.Write(siloAddress);
            }

            writer.Commit();

            var length = outputWriter.CommittedBytes;

            if (length > MaxPreambleLength)
            {
                throw new InvalidOperationException($"Created preamble of length {length}, which is greater than maximum allowed size of {MaxPreambleLength}.");
            }

            WriteLength(outputWriter, length);

            var flushResult = await output.FlushAsync();

            if (flushResult.IsCanceled)
            {
                throw new OperationCanceledException("Flush canceled");
            }

            return;
        }