  Public Function SFTP(pURL As String, pUser As String, pPswd As String, pFFn As String, _
                         Optional ByRef pOutReason As String = Nil)
        Dim i As Integer = 0, i2 As Integer = 0, i3 As Integer = 0
        Dim s As String = Nil, Ss As String = Nil, Sss As String = Nil
        '  http://winscp.net/eng/docs/guide_dotnet
        Dim FunRC As Boolean
        Dim DidHostRetry As Boolean
        Dim logname As String = ProdPath & Program & "FTPlog.xml"
        If MyTesting Then logname = "z:\" & Program & "FTPlog.xml"
        Static SaveURL As String = Nil
        On Error GoTo ErrX

        If pURL = Nil Then
            pOutReason = "URL is blank "
            GoTo ExitFunction
        End If
        If pPswd = Nil Then
            pOutReason = "Password is blank "
            GoTo ExitFunction
        End If
        If pUser = Nil Then
            pOutReason = "User is blank "
            GoTo ExitFunction
        End If
        If pFFn = Nil Then
            pOutReason = "file name is blank "
            GoTo ExitFunction
        End If
        If Not My.Computer.FileSystem.FileExists(logname) Then
            i = FreeFile()
            FileOpen(i, logname, OpenMode.Output)
            FileClose(i)

        End If

        Dim hostKey As String = Nil
        If FormMain.chkFTPDoGetFingerPrint.Checked Then
            If pURL <> SaveURL OrElse (hostKey <> Nil AndAlso hostKey <> "*") Then
                FormMain.lblMsg.Text = "Getting URL fingerprint " & pURL : FormMain.lblMsg.Refresh()
                ' *********************************************************************
                hostKey = GetAndStoreFingerprint(pHost:=pURL, pLogName:=logname, pOutReason:=pOutReason)
                ' *********************************************************************
                Sss = "NOT obtained, continuing"
                If hostKey <> Nil Then Sss = "obtained "
                FormMain.lblMsg.Text = "URL fingerprint " & Sss & " " & pURL : FormMain.lblMsg.Refresh()
            End If
        End If

        If hostKey = Nil Then
            hostKey = "*"
            'GoTo exitfunction
        End If

RetryHost:
        Dim OpenConnectStr As String = "open sftp://" & pUser & ":" & pPswd & "@" & pURL & ":22999 " '   -hostkey=" & DQ & hostKey & DQ
        If hostKey <> Nil Then
            OpenConnectStr &= " -hostkey=" & DQ & hostKey & DQ
        End If
        '  My.Computer.FileSystem.WriteAllText(logname, "", True)


        ' Run hidden WinSCP process
        Dim winscp As Process = New Process()
        winscp.StartInfo.FileName = AaaTeXProdExePath & "\winscp.com"
        winscp.StartInfo.Arguments = "/xmllog=" + logname
        winscp.StartInfo.UseShellExecute = False
        winscp.StartInfo.RedirectStandardInput = True
        winscp.StartInfo.RedirectStandardOutput = True
        winscp.StartInfo.CreateNoWindow = True

        ' winscp.StartInfo.host = pURL
        Dim process As New Process

        If Not winscp.Start() Then
            i = i
            pOutReason = "winSCP start failed "
        End If

        ' Feed in the scripting commands
        winscp.StandardInput.WriteLine(OpenConnectStr)
        winscp.StandardInput.WriteLine("option batch continue")  ' abort")
        winscp.StandardInput.WriteLine("option confirm off")
        winscp.StandardInput.WriteLine("open mysession")

        ' winscp.StandardInput.WriteLine("port 22999")
        winscp.StandardInput.WriteLine("ls")
        winscp.StandardInput.WriteLine("put " & pFFn)

        winscp.StandardInput.Close()

        ' Collect all output (not used in this example)
        Dim output As String = winscp.StandardOutput.ReadToEnd()

        ' Wait until WinSCP finishes
        FormMain.lblMsg.Text = "Waiting for WinSCP " : FormMain.lblMsg.Refresh()
        winscp.WaitForExit()
        FormMain.lblMsg.Text = "Return from WinSCP " : FormMain.lblMsg.Refresh()
        fnDebug(output)
        Const PutCon As String = "winscp> put "
        '  c:\lxcg.log               |         18 KiB |   44.5 KiB/s | binary | 100%
        If Trim(output) = Nil Then
            pOutReason = "WinSCP output is blank "
            GoTo exitfunction


        ElseIf output.Contains("Host key wasn") AndAlso Not DidHostRetry Then
            'The server's dss key fingerprint is:  ssh-dss 1024 27:17:be:cd:17:b7:ed:48:92:b8:c5:7c:0a:43:19:59  If you trust this host
            i = i
            DidHostRetry = True
            Const FingerPrintCon As String = "dss key fingerprint is:", SSHCon As String = "ssh-dss "
            i = InStr(output, FingerPrintCon)
            If i > 0 Then
                i += Len(FingerPrintCon)
                i2 = InStr(i, output, SSHCon)
                If i2 > 0 Then
                    i3 = InStr(i2, output, vbCr)
                    If i3 = 0 Then i3 = InStr(i2, output, SP)

                    hostKey = Trim(Mid(output, i2, i3 - i2))
                    i = i
                    FormMain.lblMsg.Text = "Retring WinSCP with fingerprint " : FormMain.lblMsg.Refresh()
                    GoTo RetryHost

                End If
            End If
            'winscp.StandardInput.WriteLine("Y")
            'winscp.StandardInput.Close()
            'winscp.WaitForExit()

        ElseIf output.Contains("Cannot start") Then
            pOutReason = "winSCP cannot start " & output
            GoTo ExitFunction

        ElseIf output.Contains(PutCon) AndAlso output.Contains(pFFn) Then   '  Must be after the others
            'winscp> put c:\lxcg.log

            '  c:\lxcg.log               |         18 KiB |   44.5 KiB/s | binary | 100%

            i2 = output.IndexOf(PutCon) : If i2 <= 0 Then pOutReason = "Cannot find in output " & PutCon : GoTo ExitFunction
            i2 += Len(PutCon)
            i2 = output.IndexOf(pFFn, i2) : If i2 <= 0 Then pOutReason = "Cannot find in output file name=" & pFFn : GoTo ExitFunction
            i2 = output.IndexOf(pFFn, i2 + 5) : If i2 <= 0 Then pOutReason = "Cannot find in output file name=" & pFFn : GoTo ExitFunction
            i = output.IndexOf("%", i2)
            If i = 0 Then
                pOutReason &= "Output has file name but no %"
                GoTo exitfunction
            End If
            i3 = output.IndexOf(vbCr, i2)
            Ss = Mid(output, i2, i3 - i2)
            fnDebug("SFTP Results " & Ss)

            sAR = Split(Ss, "|") : For i = 0 To sAR.Length - 1 : sAR(i) = Trim(sAR(i)) : Next
            If sAR.Length > 0 Then
                Sss = sAR.Last
                D = Val(Sss)
                If D = 100 Then
                    i = i
                Else
                    i = i
                End If
                FunRC = (D = 100)
                SaveURL = pURL
                If winscp.ExitCode <> 0 Then pOutReason &= "Exit code=" & winscp.ExitCode
                FormMain.lblMsg.Text = "Return from WinSCP " & Ss : FormMain.lblMsg.Refresh()
                GoTo ExitFunction
            End If

        End If
        i = i
        B = (winscp.ExitCode = 0)  '  True does not mean it worked  must check output
        If Not B Then
            i = i
            pOutReason &= "WinSCP Exit code <> 0 code=" & winscp.ExitCode
            'GoTo exitfunction
        End If
        ' Parse and interpret the XML log
        ' (Note that in case of fatal failure the log file may not exist at all)
        If Not My.Computer.FileSystem.FileExists(logname) Then
            pOutReason = "Log file does not exists file=" & logname & SP & Replace(output, NL, SP)
            ' GoTo ExitFunction
        End If
        s = My.Computer.FileSystem.ReadAllText(logname)
        If s = Nil Then
            pOutReason &= "No Log file data file=" & logname & " Output=" & Replace(output, NL, SP)
            GoTo NoLog
        End If

        Dim log As XPathDocument = New XPathDocument(logname)
        Dim ns As XmlNamespaceManager = New XmlNamespaceManager(New NameTable())
        ns.AddNamespace("w", "http://winscp.net/schema/session/1.0")
        Dim nav As XPathNavigator = log.CreateNavigator()

        ' Success (0) or error?

        If winscp.ExitCode <> 0 Then

            Console.WriteLine("Error occured")

            ' See if there are any messages associated with the error
            For Each message As XPathNavigator In nav.Select("//w:message", ns)
                Console.WriteLine(message.Value)
            Next

        Else

            ' It can be worth looking for directory listing even in case of
            ' error as possibly only upload may fail

            Dim files As XPathNodeIterator = nav.Select("//w:file", ns)
            Console.WriteLine(String.Format("There are {0} files and subdirectories:", files.Count))
            For Each file As XPathNavigator In files
                Console.WriteLine(file.SelectSingleNode("w:filename/@value", ns).Value)
            Next

        End If
NoLog:
        FunRC = True

ExitFunction:
        fnDebug("SFTP Exit " & FunRC & SP & pOutReason)
        Return FunRC
ErrX:
        Ern = Err.Number : Erd = Err.Description
        Msg = "btn Export Click Err=" & Ern & SP & Erd
        pOutReason = Msg
        If MyTesting Then
            Stop

            Resume ExitFunction
            Resume
        End If
        MsgBox(Msg)
        Resume ExitFunction

    End Function



Private Function GetAndStoreFingerprint(ByRef pHost As String, _
                            pLogName As String, Optional ByRef pOutReason As String = Nil) As String

        Dim i As Integer = 0
        Dim s As String = Nil, Ss As String = Nil
        Dim FunRC As String = Nil
        On Error GoTo ErrX
        retB = 0 : pOutReason = Nil
        'attempt a connect and load the fingerprint from initial connect.
        Dim startInfo As New ProcessStartInfo
        startInfo.FileName = AaaTeXProdExePath & "WinSCP.com"
        startInfo.Arguments = "/xmllog=" + pLogName
        startInfo.RedirectStandardInput = True
        startInfo.RedirectStandardOutput = True
        startInfo.UseShellExecute = False
        startInfo.CreateNoWindow = True
        startInfo.WindowStyle = ProcessWindowStyle.Hidden

        Dim process As New Process
        process.StartInfo = startInfo
        process.Start()
        process.StandardInput.WriteLine("open " & pHost)
        process.StandardInput.Close()
        process.WaitForExit()

        'read the first line from the output of the command.
        Dim response As String = process.StandardOutput.ReadLine
        Dim rsa As String = ""

        'loop through all lines of the output for the line containing
        'the servers fingerprint (hostkey)
        s = Nil
        While response <> Nothing
            If response.Contains("ssh-rsa") Then
                rsa = response
                Exit While
            End If
            s &= response & SP
            response = process.StandardOutput.ReadLine
        End While

        'return the key
        If rsa = Nil Then pOutReason = s
        Return rsa



        'return not found
        Return ""


ExitFunction:
        Return FunRC

ErrX:
        Ern = Err.Number : Erd = Err.Description
        Msg = "btn Export Click Err=" & Ern & SP & Erd
        FormMain.lblMsg.Text = Msg
        If MyTesting Then
            Stop

            Resume ExitFunction
            Resume
        End If
        MsgBox(Msg)
        Resume ExitFunction
    End Function