protected override void DispatchScript(System.Net.Sockets.TcpClient client)
        {
            System.Threading.Thread t = new System.Threading.Thread(delegate()
            {
                using (Stream stream = client.GetStream())
                {
                    StreamAGIChannel agiChannel = new StreamAGIChannel(stream);

                    AGIScriptHost host = new AGIScriptHost(agiChannel, this);

                    AGIRequestInfo requestInfo = new AGIRequestInfo(host.GetAGIVariables());

                    IRemoteScriptManager remoteAGIScriptManager = getRequestToken(requestInfo);

                    if (remoteAGIScriptManager == null)
                    {
                        return;
                    }

                    try
                    {
                        remoteAGIScriptManager.Execute(host, getManagerConfigurationSettings(requestInfo));
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine(string.Format("A script failed with the following exception: {0}", exc.ToString()));
                    }
                    finally
                    {
                        remoteAGIScriptManager = null;
                    }
                }

                client.Close();
            });

            t.Start();
        }
        private IRemoteScriptManager getRequestToken(AGIRequestInfo requestInfo)
        {
            Uri uri = new Uri(requestInfo.Request);

            IRemoteScriptManager manager = null;

            if (_scriptMappings.ContainsKey(uri.AbsolutePath))
            {
                AGIScriptMappingElement scriptMapping = ScriptHostRuntime.AGIScriptHostConfiguration.AGIScriptMappings[uri.AbsolutePath];

                AddInToken remoteManagerToken = getRemoteScriptManagerToken(scriptMapping.ScriptManagerName);

                AppDomain targetDomain = AppDomain.CurrentDomain;

                if (scriptMapping.IsolationMode == ScriptIsolationMode.AppDomain)
                {
                    PermissionSet grantPermissionSet = getPermissionSet(scriptMapping);

                    AppDomainSetup setup = new AppDomainSetup();
                    setup.ShadowCopyFiles = "true";
                    setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

                    targetDomain = AppDomain.CreateDomain(uri.AbsolutePath, null, setup, grantPermissionSet);
                    manager      = remoteManagerToken.Activate <IRemoteScriptManager>(targetDomain);
                }
                else if (scriptMapping.IsolationMode == ScriptIsolationMode.Process)
                {
                    manager = remoteManagerToken.Activate <IRemoteScriptManager>(new AddInProcess(), getPermissionSet(scriptMapping));
                }
                else
                {
                    manager = remoteManagerToken.Activate <IRemoteScriptManager>(targetDomain);
                }
            }

            return(manager);
        }