public static WorkstationCommandBase ParseFactory(InputByteArray InputArray)
        {
            WorkstationCommandBase dsCmd = null;

            if (InputArray.RemainingLength >= 2)
            {
                // command codes as documented on 15.2 - 1 of 5494 function ref manual
                var buf = InputArray.PeekBytes(2);
                if (buf[0] == 0x04)
                {
                    var wrkstnCode = buf[1].ToWorkstationCode();
                    if (wrkstnCode != null)
                    {
                        var code = wrkstnCode.Value;
                        if (code == WorkstationCode.ClearUnit)
                        {
                            dsCmd = new ClearUnitCommand(InputArray);
                        }

                        else if (code == WorkstationCode.WTD)
                        {
                            dsCmd = new WriteToDisplayCommand(InputArray);
                        }

                        else if (code == WorkstationCode.ReadMdtFields)
                        {
                            dsCmd = new ReadMdtFieldsCommand(InputArray);
                        }

                        // 04 F3. write structure field. Used as vehicle for the D9 70 5250 query
                        // command.
                        else if (code == WorkstationCode.WriteStructuredField)
                        {
                            dsCmd = new WriteStructuredFieldCommand(InputArray);
                        }

                        else if (code == WorkstationCode.SaveScreen)
                        {
                            dsCmd = new SaveScreenCommand(InputArray);
                        }

                        else if (code == WorkstationCode.RestoreScreen)
                        {
                            dsCmd = new RestoreScreenCommand(InputArray);
                        }

                        else
                        {
                            throw new Exception(
                                      "workstation data stream command code not supported. " +
                                      code.ToString());
                        }
                    }
                }
            }

            return(dsCmd);
        }
        public static byte[] BuildSaveScreenResponse(ScreenContent ScreenContent)
        {
            var ra = new ByteArrayBuilder();

            // data stream header.
            {
                var buf = DataStreamHeader.Build(50, TerminalOpcode.SaveScreen, 0, 0);
                ra.Append(buf);
            }

            // restore screen workstation command.
            {
                var cmd = new RestoreScreenCommand();
                ra.Append(cmd.ToBytes());
            }

            // clear unit command.
            if (ScreenContent.ScreenDim.GetIsWideScreen( ) == true)
            {
                var cmd = new ClearUnitAlternateCommand(0x00);
                ra.Append(cmd.ToBytes());
            }
            else
            {
                var cmd = new ClearUnitCommand();
                ra.Append(cmd.ToBytes());
            }

            // WTD command.
            {
                var ordersByteStream = ScreenContent.BuildOrderStream( );
                var buf = WriteToDisplayCommand.Build(0x00, 0x18, ordersByteStream);
                ra.Append(buf);
            }

            // update length of response data stream.
            {
                var wk = new ByteArrayBuilder();
                wk.AppendBigEndianShort((short)ra.Length);
                ra.CopyToBuffer(wk.ToByteArray(), 0);
            }

            // IAC EOR
            {
                ra.Append(EOR_Command.Build());
            }

            return(ra.ToByteArray());
        }