private void AddErrorNicely(StringList sw, Exception ex)
        {
            sw.Write(string.Format("Runtime error ({0}): {1}", ex.GetType().Name, ex.Message));

            string error = _py.GetStackTraceFromException(ex);

            error = error.Replace(", in <module>, \"<string>\"", ", in script");
            error = error.Trim();

            sw.Write(error);
        }
        // the main listener function
        protected void TcpServerWorkerListening(object sender, DoWorkEventArgs e)
        {
            //---listen at the specified IP and port no.---
            const int portNo   = 614;
            IPAddress serverIp = IPAddress.Parse("127.0.0.1");

            if (_server == null)
            {
                _server = new TcpListener(serverIp, portNo);
            }
            try
            {
                _server.Start();
                RhinoApp.WriteLine("VS Code Listener Started...");
            }
            catch (Exception err)
            {
                RhinoApp.WriteLine(err.ToString());
            }

            while (true)
            {
                // incoming client connected
                TcpClient client;
                try
                {
                    client = _server.AcceptTcpClient();
                }
                catch (Exception serverException)
                {
                    return;
                }

                // get the incoming data through a network stream
                NetworkStream nwStream = client.GetStream();
                byte[]        buffer   = new byte[client.ReceiveBufferSize];

                // read incoming stream
                int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);

                // convert the data received into a string
                StringBuilder msg = new StringBuilder();

                // parse the buffer into msg
                foreach (var b in buffer)
                {
                    if (b.Equals(00))
                    {
                        break;
                    }
                    msg.Append(Convert.ToChar(b).ToString());
                }

                // parse the received message into C# Object
                string msgString = msg.ToString();
                msgString = Regex.Split(msgString, "}")[0] + "}";
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(msgString));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(msgObject));
                msgObject msgObj;
                try
                {
                    msgObj = ser.ReadObject(ms) as msgObject;
                }
                catch (Exception ex)
                {
                    RhinoApp.WriteLine("Received invalid data, please try again.");
                    return;
                }

                ms.Close();

                // invoke the main task in the main thread
                Application.Current.Dispatcher.Invoke(() =>
                {
                    // create python script runner
                    PythonScript myScript = PythonScript.Create();
                    // redirect output to _output field
                    myScript.Output = PrintToVSCode;
                    FeedbackSender feedbackSender = new FeedbackSender(nwStream);
                    GotCodeFeekBack += feedbackSender.OnGotCodeFeedBack;

                    // if flagged reset, then reset the script engine.
                    if (msgObj.reset)
                    {
                        ResetScriptEngine.ResetEngine();
                    }

                    // if it is not a temp folder, add the folder to python library path
                    if (!msgObj.temp)
                    {
                        string pythonFilePath = Path.GetDirectoryName(msgObj.filename);
                        string code           = string.Format("import sys\nimport os\nif \"{0}\" not in sys.path: sys.path.append(\"{0}\")", pythonFilePath);
                        try
                        {
                            myScript.ExecuteScript(code);
                        }
                        catch (Exception exception)
                        {
                            PrintToVSCode(exception.Message);
                        }
                    }

                    // determines if run actual script
                    if (msgObj.run)
                    {
                        uint sn      = _idoc.BeginUndoRecord("VS Code execution");
                        var sn_start = RhinoObject.NextRuntimeSerialNumber;
                        try
                        {
                            if (msgObj.minimize)
                            {
                                Utils.MinimizeVSCodeWindow();
                            }

                            myScript.ExecuteFile(msgObj.filename);
                        }
                        catch (Exception ex)
                        {
                            // get the exception message
                            var error      = myScript.GetStackTraceFromException(ex);
                            string message = ex.Message + "\n" + error;
                            // send exception msg back to VS Code
                            PrintToVSCode(message);
                        }
                        finally
                        {
                            CloseConnection(nwStream);
                            _idoc.EndUndoRecord(sn);
                            if (msgObj.minimize)
                            {
                                Utils.RestoreVSCodeWindow();
                            }
                            // fix the rs.Prompt bug
                            RhinoApp.SetCommandPrompt("Command");

                            // select created objects
                            var sn_end = RhinoObject.NextRuntimeSerialNumber;
                            if (sn_end > sn_start)
                            {
                                for (var i = sn_start; i < sn_end; i++)
                                {
                                    var obj = _idoc.Objects.Find(i);
                                    if (null != obj)
                                    {
                                        obj.Select(true);
                                    }
                                }
                            }
                            // enable the view
                            _idoc.Views.RedrawEnabled = true;
                        }
                    }
                    else
                    {
                        CloseConnection(nwStream);
                    }
                });
            }
        }