/// <summary>
        /// Parallel thread - listener.
        /// </summary>
        private void ServerListenerProc()
        {
            Thread.CurrentThread.Name = "External Debugging Listener";
            ProfilerShort.Autocommit  = false;

            m_listener = new TcpListener(IPAddress.Any, GameDebugPort)
            {
                ExclusiveAddressUse = false
            };
            try
            {
                m_listener.Start();
            }
            catch (SocketException e)
            {
                MyLog.Default.WriteLine("Cannot start debug listener.");
                MyLog.Default.WriteLine(e);
                m_listener = null;
                m_active   = false;
                return;
            }
            MyLog.Default.WriteLine("External debugger: listening...");
            while (true)
            {
                try
                {
                    var client = m_listener.AcceptTcpClient();
                    client.Client.Blocking = true;
                    MyLog.Default.WriteLine("External debugger: accepted client.");
                    m_clients.Add(new MyDebugClientInfo()
                    {
                        TcpClient  = client,
                        LastHeader = MyExternalDebugStructures.CommonMsgHeader.Create("UNKNOWN")
                    });
                    m_clients.ApplyAdditions();
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.Interrupted)
                    {
                        m_listener.Stop();
                        m_listener = null;
                        MyLog.Default.WriteLine("External debugger: interrupted.");
                        ProfilerShort.Commit();
                        ProfilerShort.DestroyThread();
                        return;
                    }
                    else
                    {
                        MyLog.Default.WriteLine(e);
                        break;
                    }
                }
            }
            m_listener.Stop();
            m_listener = null;
            ProfilerShort.Commit();
            ProfilerShort.DestroyThread();
        }
        void ThreadProc()
        {
            Thread.CurrentThread.Name = "Entity creation thread";
            HkBaseSystem.InitThread("Entity creation thread");
            ProfilerShort.Autocommit = false;
            MyEntityIdentifier.InitPerThreadStorage(2048);
            Item item;

            while (!m_exitting)
            {
                if (ConsumeWork(out item))
                {
                    if (item.ObjectBuilder != null)
                    {
                        if (item.Result == null)
                        {
                            item.Result = MyEntities.CreateFromObjectBuilderNoinit(item.ObjectBuilder);
                        }
                        item.Result.SentFromServer = true;
                        item.InScene = (item.ObjectBuilder.PersistentFlags & MyPersistentEntityFlags2.InScene) == MyPersistentEntityFlags2.InScene;
                        item.ObjectBuilder.PersistentFlags &= ~MyPersistentEntityFlags2.InScene;
                        item.Result.DebugAsyncLoading       = true;
                        MyEntities.InitEntity(item.ObjectBuilder, ref item.Result);
                        if (item.Result != null)
                        {
                            if (item.SubgridBuilders != null)
                            {
                                item.SubGrids = new List <MyEntity>();
                                foreach (var subGridbulider in item.SubgridBuilders)
                                {
                                    MyEntity subGrid = MyEntities.CreateFromObjectBuilderNoinit(subGridbulider);
                                    subGridbulider.PersistentFlags &= ~MyPersistentEntityFlags2.InScene;
                                    item.Result.DebugAsyncLoading   = true;

                                    MyEntities.InitEntity(subGridbulider, ref subGrid);
                                    item.SubGrids.Add(subGrid);
                                }
                                item.SubgridBuilders.Clear();
                                item.SubgridBuilders = null;
                            }

                            item.EntityIds = new List <IMyEntity>();
                            MyEntityIdentifier.GetPerThreadEntities(item.EntityIds);
                            MyEntityIdentifier.ClearPerThreadEntities();
                            m_resultQueue.Enqueue(item);
                        }
                    }
                    else
                    {
                        if (item.Result != null)
                        {
                            item.Result.DebugAsyncLoading = true;
                        }

                        // This is ok, just invoking action asynchronously
                        m_resultQueue.Enqueue(item);
                    }
                }
                ProfilerShort.Commit();
            }
            MyEntityIdentifier.DestroyPerThreadStorage();
            HkBaseSystem.QuitThread();
            ProfilerShort.DestroyThread();
        }