private void CreateUploadResponse(object objState)
        {
            WinScpWebResponse wr = null;

            try
            {
                object[] v = (objState as object[]);
                if (v.Length != 4)
                {
                    Debug.Assert(false); return;
                }

                Uri    uri              = (v[0] as Uri);
                string strScript        = (v[1] as string);
                string strTempFile      = (v[2] as string);
                WebHeaderCollection whc = (v[3] as WebHeaderCollection);

                wr = new WinScpWebResponse(uri, strScript, strTempFile, false, whc);
            }
            catch (Exception exUpload) { m_exUpload = exUpload; }

            lock (objState)
            {
                m_wswrUpload           = wr;
                m_bUploadResponseReady = true;
            }
        }
        private WebResponse RunOp(string strTempFile, string strSessionUrl,
            string strRemoteDir, string strRemoteFile)
        {
            WswrOp wOp;
            if(m_strMethod == IOConnection.WrmDeleteFile) wOp = WswrOp.Delete;
            else if(m_strMethod == IOConnection.WrmMoveFile) wOp = WswrOp.Move;
            else if(m_lRequestData.Count > 0) wOp = WswrOp.Upload;
            else wOp = WswrOp.Download;

            if(wOp == WswrOp.Upload)
                File.WriteAllBytes(strTempFile, m_lRequestData.ToArray());

            StringBuilder sbHostKeyDetect = InitScript();
            sbHostKeyDetect.AppendLine(AddCommonOpenOptions("open " +
                strSessionUrl, strSessionUrl));
            sbHostKeyDetect.AppendLine("exit");

            string strHostKey = null;
            try { WinScpExecutor.RunScript(sbHostKeyDetect.ToString()); }
            catch(Exception exProbe)
            {
                strHostKey = GetHostKey(exProbe.Message);
                if(string.IsNullOrEmpty(strHostKey)) throw;
            }

            string strScript;
            if(wOp == WswrOp.Download) // Test file exists
            {
                strScript = BuildScript(WswrOp.Download, true, strTempFile,
                    strSessionUrl, strRemoteDir, strRemoteFile, strHostKey);
                string strResult = WinScpExecutor.RunScript(strScript);

                strResult = strResult.Replace("\r\n", "\n");
                strResult = strResult.Replace("\r", "\n");
                string[] vLines = strResult.Split(new char[] { '\n' },
                    StringSplitOptions.RemoveEmptyEntries);
                bool bFound = false;
                foreach(string strLine in vLines)
                {
                    if((strLine == strRemoteFile) ||
                        strLine.EndsWith(" " + strRemoteFile) ||
                        strLine.EndsWith("\t" + strRemoteFile) ||
                        strLine.EndsWith("/" + strRemoteFile))
                    {
                        bFound = true;
                        break;
                    }
                }
                if(!bFound) throw new FileNotFoundException("Remote file not found!");
            }

            strScript = BuildScript(wOp, false, strTempFile, strSessionUrl,
                strRemoteDir, strRemoteFile, strHostKey);

            StatusStateInfo st = null;
            if(wOp == WswrOp.Upload)
                st = StatusUtil.Begin("Uploading file...");

            WebResponse wr;
            try
            {
                if(wOp == WswrOp.Upload)
                {
                    object[] vState = new object[4];
                    vState[0] = m_uri;
                    vState[1] = strScript;
                    vState[2] = strTempFile;
                    vState[3] = m_whcHeaders;

                    m_bUploadResponseReady = false;
                    m_exUpload = null;

                    object objState = (object)vState;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(
                        CreateUploadResponse), objState);

                    bool bReady = false;
                    while(!bReady)
                    {
                        lock(objState) { bReady = m_bUploadResponseReady; }

                        Thread.Sleep(100);
                        Application.DoEvents();
                    }

                    wr = m_wswrUpload;
                    if(m_exUpload != null) throw m_exUpload;
                }
                else
                    wr = new WinScpWebResponse(m_uri, strScript, strTempFile,
                        (wOp == WswrOp.Download), m_whcHeaders);
            }
            finally
            {
                if(wOp == WswrOp.Upload) StatusUtil.End(st);
            }

            return wr;
        }
        // private static string GetHostKey(string strError)
        // {
        //	strError = strError.Replace("\r\n", "\n");
        //	strError = strError.Replace("\r", "\n");
        //	string[] vLines = strError.Split(new char[] { '\n' },
        //		StringSplitOptions.RemoveEmptyEntries);
        //	foreach(string strLineIt in vLines)
        //	{
        //		if(strLineIt.StartsWith("ssh-rsa")) return strLineIt;
        //		if(strLineIt.StartsWith("ssh-dss")) return strLineIt;
        //	}
        //	return null;
        // }

        private WebResponse RunOp(string strTempFile, string strSessionUrl,
                                  string strRemotePath)
        {
            WswrOp wOp;

            if (m_strMethod == IOConnection.WrmDeleteFile)
            {
                wOp = WswrOp.Delete;
            }
            else if (m_strMethod == IOConnection.WrmMoveFile)
            {
                wOp = WswrOp.Move;
            }
            else if (m_lRequestData.Count > 0)
            {
                wOp = WswrOp.Upload;
            }
            else
            {
                wOp = WswrOp.Download;
            }

            if (wOp == WswrOp.Upload)
            {
                File.WriteAllBytes(strTempFile, m_lRequestData.ToArray());
            }

            // StringBuilder sbHostKeyDetect = InitScript();
            // sbHostKeyDetect.AppendLine(AddCommonOpenOptions(
            //	"open " + strSessionUrl, strSessionUrl, string.Empty));
            // sbHostKeyDetect.AppendLine("exit");
            // string strHostKey = null;
            // try { WinScpExecutor.RunScript(sbHostKeyDetect.ToString()); }
            // catch(Exception exProbe)
            // {
            //	strHostKey = GetHostKey(exProbe.Message);
            //	if(string.IsNullOrEmpty(strHostKey)) throw;
            // }

            string strScript;

            if (wOp == WswrOp.Download)            // Test file exists
            {
                strScript = BuildScript(WswrOp.Download, true, strTempFile,
                                        strSessionUrl, strRemotePath);
                string strResult = WinScpExecutor.RunScript(strScript);

                string strRemoteFile = UrlUtil.GetFileName(strRemotePath);

                strResult = strResult.Replace("\r\n", "\n");
                strResult = strResult.Replace("\r", "\n");
                string[] vLines = strResult.Split(new char[] { '\n' },
                                                  StringSplitOptions.RemoveEmptyEntries);
                bool bFound = false;
                foreach (string strLine in vLines)
                {
                    if ((strLine == strRemoteFile) ||
                        strLine.EndsWith(" " + strRemoteFile) ||
                        strLine.EndsWith("\t" + strRemoteFile) ||
                        strLine.EndsWith("/" + strRemoteFile))
                    {
                        bFound = true;
                        break;
                    }
                }
                if (!bFound)
                {
                    throw new FileNotFoundException("Remote file not found!");
                }
            }

            strScript = BuildScript(wOp, false, strTempFile, strSessionUrl,
                                    strRemotePath);

            StatusStateInfo st = null;

            if (wOp == WswrOp.Upload)
            {
                st = StatusUtil.Begin("Uploading file...");
            }

            WebResponse wr;

            try
            {
                if (wOp == WswrOp.Upload)
                {
                    object[] vState = new object[4];
                    vState[0] = m_uri;
                    vState[1] = strScript;
                    vState[2] = strTempFile;
                    vState[3] = m_whcHeaders;

                    m_bUploadResponseReady = false;
                    m_exUpload             = null;

                    object objState = (object)vState;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(
                                                     CreateUploadResponse), objState);

                    bool bReady = false;
                    while (!bReady)
                    {
                        lock (objState) { bReady = m_bUploadResponseReady; }

                        Thread.Sleep(100);
                        Application.DoEvents();
                    }

                    wr = m_wswrUpload;
                    if (m_exUpload != null)
                    {
                        throw m_exUpload;
                    }
                }
                else
                {
                    wr = new WinScpWebResponse(m_uri, strScript, strTempFile,
                                               (wOp == WswrOp.Download), m_whcHeaders);
                }
            }
            finally
            {
                if (wOp == WswrOp.Upload)
                {
                    StatusUtil.End(st);
                }
            }

            return(wr);
        }
        private void CreateUploadResponse(object objState)
        {
            WinScpWebResponse wr = null;

            try
            {
                object[] v = (objState as object[]);
                if(v.Length != 4) { Debug.Assert(false); return; }

                Uri uri = (v[0] as Uri);
                string strScript = (v[1] as string);
                string strTempFile = (v[2] as string);
                WebHeaderCollection whc = (v[3] as WebHeaderCollection);

                wr = new WinScpWebResponse(uri, strScript, strTempFile, false, whc);
            }
            catch(Exception exUpload) { m_exUpload = exUpload; }

            lock(objState)
            {
                m_wswrUpload = wr;
                m_bUploadResponseReady = true;
            }
        }