/// <summary>
 /// Thread which extracts commands from commandsQueue queue and sends them to SP.
 /// </summary>
 private void outThread()
 {
     //An object to be sent (command).
     AltiLinkPlus.ALPCommand cmd       = null;
     Diacom.Cmd.CmdBase      spCommand = null;
     // While outThreadLivingStatus == true we should dequeue commands from outgoing queue and send them.
     while (outThreadLivingStatus)
     {
         try
         {
             // Getting next command and processing it.
             object obj = commandsQueue.Dequeue();
             // First try as a raw ALP command
             cmd = obj as AltiLinkPlus.ALPCommand;
             // If not ALP command - convert to
             if (cmd == null)
             {
                 spCommand = obj as Diacom.Cmd.CmdBase;
                 cmd       = ProcessCommand(spCommand);
             }
             // If not null then command is valid - sending it.
             if (cmd != null)
             {
                 // Creating new packet.
                 AltiLinkPlus.ALPPacket outThreadPacket = new AltiLinkPlus.ALPPacket();
                 // Putting command into a packet.
                 outThreadPacket.Add(cmd);
                 TraceOut.Put("AltiGenSPCore::ProcessCommand LocId=" + cmd.LocationId + " CmdId=" + ((ALPCmdID)cmd.CommandId).ToString() + " SeqId=" + cmd.SequenceId);
                 // Sending out the packet.
                 outThreadPacket.Write(bw);
             }
         }
         catch (Exception x)
         {
             if (outThreadLivingStatus)
             {
                 TraceOut.Put(x);
                 RaiseEvent(SPStatus.ERROR_CONNECTION, String.Format("Sending command: {0}", x.Message));
             }
         }
     }
 }
 /// <summary>
 /// Thread which receives packets from AltiGen Server and
 /// calls the appropriate procedures - to process event or response.
 /// </summary>
 private void inThread()
 {
     AltiLinkPlus.ALPPacket inThreadPacket = new AltiLinkPlus.ALPPacket();
     // While inThreadLivingStatus == true we should receive incoming packets.
     while (inThreadLivingStatus)
     {
         try
         {
             // Reding next packet.
             inThreadPacket.Read(br);
             // Loop through every block of data in the received packet.
             for (int j = 0; j < inThreadPacket.BlockCount; j++)
             {
                 // Getting block.
                 AltiLinkPlus.ALPDataBlock dataBlock = inThreadPacket.PacketData[j];
                 if (dataBlock is AltiLinkPlus.ALPEvent)
                 {
                     // Process received event.
                     ProcessEvent((AltiLinkPlus.ALPEvent)dataBlock);
                 }
                 else if (dataBlock is AltiLinkPlus.ALPResponse)
                 {
                     // Process received response.
                     ProcessResponse((AltiLinkPlus.ALPResponse)dataBlock);
                 }
             }
         }
         catch (Exception x)
         {
             if (inThreadLivingStatus)
             {
                 TraceOut.Put(x);
                 RaiseEvent(SPStatus.ERROR_CONNECTION, x.Message);
             }
         }
     }
 }