'  
'   Rebex Sample Code License
' 
'   Copyright 2026, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.IO
Imports System.Xml

Imports Rebex.Net

''' <summary>
''' The Connection class provides user interface for setting the ssh connection settings.
''' </summary>
Public Class Connection
    Inherits Form
    Public Const DefaultProxyPort As Integer = 3128

    Public Const DefaultHostName As String = "test.rebex.net"
    Public Const DefaultUserName As String = "demo"
    Public Const DefaultPassword As String = "password"

    Public Enum ProtocolType
        Ssh = 0
        Telnet = 1
    End Enum

    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.IContainer = Nothing
    Private btnConnect As System.Windows.Forms.Button
    Private groupBox2 As System.Windows.Forms.GroupBox
    Private label10 As System.Windows.Forms.Label
    Private tbProxyPassword As System.Windows.Forms.TextBox
    Private label9 As System.Windows.Forms.Label
    Private tbProxyLogin As System.Windows.Forms.TextBox
    Private label8 As System.Windows.Forms.Label
    Private label7 As System.Windows.Forms.Label
    Private cbProxyType As System.Windows.Forms.ComboBox
    Private tbProxyPort As System.Windows.Forms.TextBox
    Private label6 As System.Windows.Forms.Label
    Private cbProxy As System.Windows.Forms.CheckBox
    Private tbProxy As System.Windows.Forms.TextBox
    Private label5 As System.Windows.Forms.Label
    Private groupBox1 As System.Windows.Forms.GroupBox
    Private tbPort As System.Windows.Forms.TextBox
    Private label2 As System.Windows.Forms.Label
    Private tbHost As System.Windows.Forms.TextBox
    Private label1 As System.Windows.Forms.Label
    Private statusStrip As System.Windows.Forms.StatusStrip
    Private statusLabel As System.Windows.Forms.ToolStripStatusLabel
    Private btnPrivateKey As System.Windows.Forms.Button
    Private tbPrivateKey As System.Windows.Forms.TextBox
    Private label11 As System.Windows.Forms.Label
    Private tbPassword As System.Windows.Forms.TextBox
    Private label4 As System.Windows.Forms.Label
    Private tbLogin As System.Windows.Forms.TextBox
    Private label3 As System.Windows.Forms.Label
    Private openDialog As System.Windows.Forms.OpenFileDialog
    Private cbProtocol As System.Windows.Forms.ComboBox
    Private label0 As System.Windows.Forms.Label

#Region "Properties"

    ''' <summary>
    ''' Gets the value indicating whether the form was confirmed.
    ''' </summary>
    Public ReadOnly Property OK() As Boolean
        Get
            Return _ok
        End Get
    End Property
    Private _ok As Boolean = False

    Private _enableTelnetLoginInfo As Boolean

    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property EnableTelnetLoginInfo() As Boolean
        Get
            Return _enableTelnetLoginInfo
        End Get
        Set
            _enableTelnetLoginInfo = Value
            tbLogin.Enabled = tbLogin.Enabled Or _enableTelnetLoginInfo
            tbPassword.Enabled = tbPassword.Enabled Or _enableTelnetLoginInfo
        End Set
    End Property

    ''' <summary>
    ''' Gets the protocol type.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property Protocol() As ProtocolType
        Get
            If cbProtocol.SelectedIndex = 0 Then
                Return ProtocolType.Ssh
            Else
                Return ProtocolType.Telnet
            End If
        End Get
        Set
            If Value = ProtocolType.Ssh Then
                cbProtocol.SelectedIndex = 0
            Else
                cbProtocol.SelectedIndex = 1
            End If
        End Set
    End Property

    ''' <summary>
    ''' Gets the host name.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property HostName() As String
        Get
            Return tbHost.Text
        End Get
        Set
            tbHost.Text = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets the port of the host.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property Port() As Integer
        Get
            Dim serverPort As Integer = 0

            Try
                serverPort = Int32.Parse(tbPort.Text)
            Catch
            End Try

            Return serverPort
        End Get
        Set
            tbPort.Text = Value.ToString()
        End Set
    End Property

    ''' <summary>
    ''' Gets the login name.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property LoginName() As String
        Get
            Return tbLogin.Text
        End Get
        Set
            tbLogin.Text = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets the password.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property Password() As String
        Get
            Return tbPassword.Text
        End Get
        Set
            tbPassword.Text = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets the file location of the private key.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property PrivateKey() As String
        Get
            Return tbPrivateKey.Text
        End Get
        Set
            tbPrivateKey.Text = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets the value indicating whether the proxy is enabled.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property ProxyEnabled() As Boolean

        Get
            Return cbProxy.Checked
        End Get
        Set
            If Value Then
                tbProxy.Enabled = True
                tbProxyPort.Enabled = True
                cbProxy.Checked = True
                cbProxyType.Enabled = True
                tbProxyLogin.Enabled = True
                tbProxyPassword.Enabled = True
            Else
                tbProxy.Enabled = False
                tbProxyPort.Enabled = False
                cbProxy.Checked = False
                cbProxyType.Enabled = False
                tbProxyLogin.Enabled = False
                tbProxyPassword.Enabled = False
            End If
        End Set
    End Property

    ''' <summary>
    ''' Gets the proxy server name.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property ProxyHostName() As String
        Get
            Return tbProxy.Text
        End Get
        Set
            tbProxy.Text = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets the port of the proxy server.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property ProxyPort() As Integer
        Get
            Dim proxyPort__1 As Integer = 0

            Try
                proxyPort__1 = Int32.Parse(tbProxyPort.Text)
            Catch
            End Try

            Return proxyPort__1
        End Get
        Set
            tbProxyPort.Text = Value.ToString()
        End Set
    End Property

    ''' <summary>
    ''' Gets the proxy login name.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property ProxyLoginName() As String
        Get
            Return tbProxyLogin.Text
        End Get
        Set
            tbProxyLogin.Text = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets the proxy password.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property ProxyPassword() As String
        Get
            Return tbProxyPassword.Text
        End Get
        Set
            tbProxyPassword.Text = Value
        End Set
    End Property

    ''' <summary>
    ''' Gets the proxy type.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property ProxyType() As ProxyType
        Get
            Select Case cbProxyType.SelectedIndex
                Case 1
                    Return ProxyType.Socks4a
                Case 2
                    Return ProxyType.Socks5
                Case 3
                    Return ProxyType.HttpConnect
                Case Else
                    Return ProxyType.Socks4
            End Select
        End Get
        Set
            Select Case Value
                Case ProxyType.Socks4a
                    cbProxyType.SelectedIndex = 1
                    Exit Select
                Case ProxyType.Socks5
                    cbProxyType.SelectedIndex = 2
                    Exit Select
                Case ProxyType.HttpConnect
                    cbProxyType.SelectedIndex = 3
                    Exit Select
                Case Else
                    cbProxyType.SelectedIndex = 0
                    Exit Select
            End Select
        End Set
    End Property

#End Region

    ''' <summary>
    ''' Initialize this class.
    ''' </summary>
    Public Sub New()
        InitializeComponent()

        ' set default values
        tbPort.Text = Ssh.DefaultPort.ToString()
        tbProxyPort.Text = DefaultProxyPort.ToString()

        ' set form values according to the config file
        SetFormValues()
    End Sub

    ''' <summary>
    ''' Clean up any resources being used.
    ''' </summary>
    ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    Protected Overrides Sub Dispose(disposing As Boolean)
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

#Region "Windows Form Designer generated code"

    ''' <summary>
    ''' Required method for Designer support - do not modify
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
        Me.btnConnect = New System.Windows.Forms.Button()
        Me.groupBox2 = New System.Windows.Forms.GroupBox()
        Me.tbProxyPassword = New System.Windows.Forms.TextBox()
        Me.tbProxyLogin = New System.Windows.Forms.TextBox()
        Me.cbProxyType = New System.Windows.Forms.ComboBox()
        Me.tbProxy = New System.Windows.Forms.TextBox()
        Me.label10 = New System.Windows.Forms.Label()
        Me.label9 = New System.Windows.Forms.Label()
        Me.label8 = New System.Windows.Forms.Label()
        Me.tbProxyPort = New System.Windows.Forms.TextBox()
        Me.label6 = New System.Windows.Forms.Label()
        Me.cbProxy = New System.Windows.Forms.CheckBox()
        Me.label5 = New System.Windows.Forms.Label()
        Me.label7 = New System.Windows.Forms.Label()
        Me.groupBox1 = New System.Windows.Forms.GroupBox()
        Me.tbPort = New System.Windows.Forms.TextBox()
        Me.label2 = New System.Windows.Forms.Label()
        Me.tbHost = New System.Windows.Forms.TextBox()
        Me.label1 = New System.Windows.Forms.Label()
        Me.btnPrivateKey = New System.Windows.Forms.Button()
        Me.tbPrivateKey = New System.Windows.Forms.TextBox()
        Me.label11 = New System.Windows.Forms.Label()
        Me.tbPassword = New System.Windows.Forms.TextBox()
        Me.label4 = New System.Windows.Forms.Label()
        Me.tbLogin = New System.Windows.Forms.TextBox()
        Me.label3 = New System.Windows.Forms.Label()
        Me.statusStrip = New System.Windows.Forms.StatusStrip()
        Me.statusLabel = New System.Windows.Forms.ToolStripStatusLabel()
        Me.openDialog = New System.Windows.Forms.OpenFileDialog()
        Me.cbProtocol = New System.Windows.Forms.ComboBox()
        Me.label0 = New System.Windows.Forms.Label()
        Me.groupBox2.SuspendLayout()
        Me.groupBox1.SuspendLayout()
        Me.statusStrip.SuspendLayout()
        Me.SuspendLayout()
        ' 
        ' btnConnect
        ' 
        Me.btnConnect.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.btnConnect.Location = New System.Drawing.Point(280, 288)
        Me.btnConnect.Name = "btnConnect"
        Me.btnConnect.Size = New System.Drawing.Size(73, 23)
        Me.btnConnect.TabIndex = 23
        Me.btnConnect.Text = "Connect"
        AddHandler Me.btnConnect.Click, New System.EventHandler(AddressOf Me.btnConnect_Click)
        ' 
        ' groupBox2
        ' 
        Me.groupBox2.Controls.Add(Me.tbProxyPassword)
        Me.groupBox2.Controls.Add(Me.tbProxyLogin)
        Me.groupBox2.Controls.Add(Me.cbProxyType)
        Me.groupBox2.Controls.Add(Me.tbProxy)
        Me.groupBox2.Controls.Add(Me.label10)
        Me.groupBox2.Controls.Add(Me.label9)
        Me.groupBox2.Controls.Add(Me.label8)
        Me.groupBox2.Controls.Add(Me.tbProxyPort)
        Me.groupBox2.Controls.Add(Me.label6)
        Me.groupBox2.Controls.Add(Me.cbProxy)
        Me.groupBox2.Controls.Add(Me.label5)
        Me.groupBox2.Controls.Add(Me.label7)
        Me.groupBox2.Location = New System.Drawing.Point(10, 148)
        Me.groupBox2.Name = "groupBox2"
        Me.groupBox2.Size = New System.Drawing.Size(342, 128)
        Me.groupBox2.TabIndex = 22
        Me.groupBox2.TabStop = False
        Me.groupBox2.Text = "Proxy settings"
        ' 
        ' tbProxyPassword
        ' 
        Me.tbProxyPassword.Location = New System.Drawing.Point(88, 96)
        Me.tbProxyPassword.Name = "tbProxyPassword"
        Me.tbProxyPassword.PasswordChar = "*"c
        Me.tbProxyPassword.Size = New System.Drawing.Size(88, 20)
        Me.tbProxyPassword.TabIndex = 29
        ' 
        ' tbProxyLogin
        ' 
        Me.tbProxyLogin.Location = New System.Drawing.Point(88, 72)
        Me.tbProxyLogin.Name = "tbProxyLogin"
        Me.tbProxyLogin.Size = New System.Drawing.Size(88, 20)
        Me.tbProxyLogin.TabIndex = 27
        ' 
        ' cbProxyType
        ' 
        Me.cbProxyType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbProxyType.Items.AddRange(New Object() {"Socks4", "Socks4a", "Socks5", "HttpConnect"})
        Me.cbProxyType.Location = New System.Drawing.Point(88, 48)
        Me.cbProxyType.Name = "cbProxyType"
        Me.cbProxyType.Size = New System.Drawing.Size(88, 21)
        Me.cbProxyType.TabIndex = 24
        ' 
        ' tbProxy
        ' 
        Me.tbProxy.Location = New System.Drawing.Point(88, 24)
        Me.tbProxy.Name = "tbProxy"
        Me.tbProxy.Size = New System.Drawing.Size(88, 20)
        Me.tbProxy.TabIndex = 20
        ' 
        ' label10
        ' 
        Me.label10.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.label10.ForeColor = System.Drawing.SystemColors.ControlDarkDark
        Me.label10.Location = New System.Drawing.Point(192, 72)
        Me.label10.Name = "label10"
        Me.label10.Size = New System.Drawing.Size(133, 48)
        Me.label10.TabIndex = 30
        Me.label10.Text = "Leave login/password fields empty when not needed."
        ' 
        ' label9
        ' 
        Me.label9.Location = New System.Drawing.Point(13, 96)
        Me.label9.Name = "label9"
        Me.label9.Size = New System.Drawing.Size(75, 23)
        Me.label9.TabIndex = 28
        Me.label9.Text = "Password:"
        ' 
        ' label8
        ' 
        Me.label8.Location = New System.Drawing.Point(13, 72)
        Me.label8.Name = "label8"
        Me.label8.Size = New System.Drawing.Size(75, 23)
        Me.label8.TabIndex = 26
        Me.label8.Text = "Login name:"
        ' 
        ' tbProxyPort
        ' 
        Me.tbProxyPort.Location = New System.Drawing.Point(248, 24)
        Me.tbProxyPort.Name = "tbProxyPort"
        Me.tbProxyPort.Size = New System.Drawing.Size(88, 20)
        Me.tbProxyPort.TabIndex = 23
        ' 
        ' label6
        ' 
        Me.label6.Location = New System.Drawing.Point(192, 24)
        Me.label6.Name = "label6"
        Me.label6.Size = New System.Drawing.Size(46, 23)
        Me.label6.TabIndex = 22
        Me.label6.Text = "Port:"
        ' 
        ' cbProxy
        ' 
        Me.cbProxy.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.cbProxy.Location = New System.Drawing.Point(248, 48)
        Me.cbProxy.Name = "cbProxy"
        Me.cbProxy.Size = New System.Drawing.Size(73, 24)
        Me.cbProxy.TabIndex = 22
        Me.cbProxy.Text = "Use proxy"
        AddHandler Me.cbProxy.Click, New System.EventHandler(AddressOf Me.cbProxy_Click)
        ' 
        ' label5
        ' 
        Me.label5.Location = New System.Drawing.Point(13, 24)
        Me.label5.Name = "label5"
        Me.label5.Size = New System.Drawing.Size(75, 23)
        Me.label5.TabIndex = 19
        Me.label5.Text = "Proxy:"
        ' 
        ' label7
        ' 
        Me.label7.Location = New System.Drawing.Point(13, 48)
        Me.label7.Name = "label7"
        Me.label7.Size = New System.Drawing.Size(75, 23)
        Me.label7.TabIndex = 25
        Me.label7.Text = "Proxy type:"
        ' 
        ' groupBox1
        ' 
        Me.groupBox1.Controls.Add(Me.label0)
        Me.groupBox1.Controls.Add(Me.cbProtocol)
        Me.groupBox1.Controls.Add(Me.tbPort)
        Me.groupBox1.Controls.Add(Me.label2)
        Me.groupBox1.Controls.Add(Me.tbHost)
        Me.groupBox1.Controls.Add(Me.label1)
        Me.groupBox1.Controls.Add(Me.btnPrivateKey)
        Me.groupBox1.Controls.Add(Me.tbPrivateKey)
        Me.groupBox1.Controls.Add(Me.label11)
        Me.groupBox1.Controls.Add(Me.tbPassword)
        Me.groupBox1.Controls.Add(Me.label4)
        Me.groupBox1.Controls.Add(Me.tbLogin)
        Me.groupBox1.Controls.Add(Me.label3)
        Me.groupBox1.Location = New System.Drawing.Point(10, 12)
        Me.groupBox1.Name = "groupBox1"
        Me.groupBox1.Size = New System.Drawing.Size(342, 128)
        Me.groupBox1.TabIndex = 21
        Me.groupBox1.TabStop = False
        Me.groupBox1.Text = "Connection settings"
        ' 
        ' tbPort
        ' 
        Me.tbPort.Location = New System.Drawing.Point(248, 46)
        Me.tbPort.Name = "tbPort"
        Me.tbPort.Size = New System.Drawing.Size(88, 20)
        Me.tbPort.TabIndex = 13
        ' 
        ' label2
        ' 
        Me.label2.Location = New System.Drawing.Point(184, 46)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(64, 23)
        Me.label2.TabIndex = 12
        Me.label2.Text = "Port:"
        ' 
        ' tbHost
        ' 
        Me.tbHost.Location = New System.Drawing.Point(88, 46)
        Me.tbHost.Name = "tbHost"
        Me.tbHost.Size = New System.Drawing.Size(88, 20)
        Me.tbHost.TabIndex = 11
        ' 
        ' label1
        ' 
        Me.label1.Location = New System.Drawing.Point(7, 46)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(81, 23)
        Me.label1.TabIndex = 10
        Me.label1.Text = "Host name:"
        ' 
        ' btnPrivateKey
        ' 
        Me.btnPrivateKey.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.btnPrivateKey.Location = New System.Drawing.Point(294, 94)
        Me.btnPrivateKey.Name = "btnPrivateKey"
        Me.btnPrivateKey.Size = New System.Drawing.Size(42, 20)
        Me.btnPrivateKey.TabIndex = 20
        Me.btnPrivateKey.Text = "..."
        AddHandler Me.btnPrivateKey.Click, New System.EventHandler(AddressOf Me.btnPrivateKey_Click)
        ' 
        ' tbPrivateKey
        ' 
        Me.tbPrivateKey.Location = New System.Drawing.Point(88, 94)
        Me.tbPrivateKey.Name = "tbPrivateKey"
        Me.tbPrivateKey.Size = New System.Drawing.Size(200, 20)
        Me.tbPrivateKey.TabIndex = 19
        ' 
        ' label11
        ' 
        Me.label11.Location = New System.Drawing.Point(7, 94)
        Me.label11.Name = "label11"
        Me.label11.Size = New System.Drawing.Size(81, 23)
        Me.label11.TabIndex = 18
        Me.label11.Text = "Private key:"
        ' 
        ' tbPassword
        ' 
        Me.tbPassword.Location = New System.Drawing.Point(248, 70)
        Me.tbPassword.Name = "tbPassword"
        Me.tbPassword.PasswordChar = "*"c
        Me.tbPassword.Size = New System.Drawing.Size(88, 20)
        Me.tbPassword.TabIndex = 17
        ' 
        ' label4
        ' 
        Me.label4.Location = New System.Drawing.Point(184, 70)
        Me.label4.Name = "label4"
        Me.label4.Size = New System.Drawing.Size(64, 23)
        Me.label4.TabIndex = 16
        Me.label4.Text = "Password:"
        ' 
        ' tbLogin
        ' 
        Me.tbLogin.Location = New System.Drawing.Point(88, 70)
        Me.tbLogin.Name = "tbLogin"
        Me.tbLogin.Size = New System.Drawing.Size(88, 20)
        Me.tbLogin.TabIndex = 15
        ' 
        ' label3
        ' 
        Me.label3.Location = New System.Drawing.Point(7, 70)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(81, 23)
        Me.label3.TabIndex = 14
        Me.label3.Text = "Login name:"
        ' 
        ' statusStrip
        ' 
        Me.statusStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.statusLabel})
        Me.statusStrip.Location = New System.Drawing.Point(0, 321)
        Me.statusStrip.Name = "statusStrip"
        Me.statusStrip.Size = New System.Drawing.Size(362, 22)
        Me.statusStrip.SizingGrip = False
        Me.statusStrip.TabIndex = 24
        '
        ' statusLabel
        '
        Me.statusLabel.Name = "statusLabel"
        Me.statusLabel.Size = New System.Drawing.Size(90, 21)
        ' 
        ' openDialog
        ' 
        Me.openDialog.Filter = "Private key (*.pri)|*.pri|Putty private key (*.ppk)|*.ppk|All files (*.*)|*.*"
        ' 
        ' cbProtocol
        ' 
        Me.cbProtocol.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbProtocol.Items.AddRange(New Object() {"SSH", "Telnet"})
        Me.cbProtocol.Location = New System.Drawing.Point(88, 19)
        Me.cbProtocol.Name = "cbProtocol"
        Me.cbProtocol.Size = New System.Drawing.Size(88, 21)
        Me.cbProtocol.TabIndex = 26
        AddHandler Me.cbProtocol.SelectedIndexChanged, New System.EventHandler(AddressOf Me.cbProtocol_SelectedIndexChanged)
        ' 
        ' label0
        ' 
        Me.label0.Location = New System.Drawing.Point(7, 19)
        Me.label0.Name = "label0"
        Me.label0.Size = New System.Drawing.Size(81, 23)
        Me.label0.TabIndex = 27
        Me.label0.Text = "Protocol:"
        ' 
        ' Connection
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(362, 343)
        Me.Controls.Add(Me.statusStrip)
        Me.Controls.Add(Me.btnConnect)
        Me.Controls.Add(Me.groupBox2)
        Me.Controls.Add(Me.groupBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.KeyPreview = True
        Me.Name = "Connection"
        Me.ShowInTaskbar = False
        Me.Text = "Connection"
        AddHandler Me.KeyDown, New System.Windows.Forms.KeyEventHandler(AddressOf Me.Connection_KeyDown)
        Me.groupBox2.ResumeLayout(False)
        Me.groupBox2.PerformLayout()
        Me.groupBox1.ResumeLayout(False)
        Me.groupBox1.PerformLayout()
        Me.statusStrip.ResumeLayout(False)
        Me.statusStrip.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    ''' <summary>
    ''' Validates the from values.
    ''' </summary>
    ''' <returns>true (everything is ok)</returns>
    Public Function ValidateForm() As Boolean
        ' host
        If tbHost.Text Is Nothing OrElse tbHost.Text.Length <= 0 Then
            statusLabel.Text = "Host name is a required field"
            Return False
        End If

        ' port
        If tbPort.Text Is Nothing OrElse tbPort.Text.Length <= 0 Then
            statusLabel.Text = "Host port is a required field"
            Return False
        End If

        ' port
        If Me.Port <= 0 Then
            statusLabel.Text = "Host port must be greater than 0"
            Return False
        End If

        ' login
        If tbLogin.Text Is Nothing OrElse tbLogin.Text.Length <= 0 Then
            statusLabel.Text = "Host login is a required field"
            Return False
        End If

        ' password
        If (tbPassword.Text Is Nothing OrElse tbPassword.Text.Length <= 0) AndAlso (tbPrivateKey.Text Is Nothing OrElse tbPrivateKey.Text.Length <= 0) Then
            statusLabel.Text = "Host password is a required field"
            Return False
        End If

        ' ----------------
        ' proxy check-outs
        ' ----------------
        If cbProxy.Checked Then
            ' proxy host
            If tbProxy.Text Is Nothing OrElse tbProxy.Text.Length <= 0 Then
                statusLabel.Text = "Proxy host is a required field when proxy is enabled"
                Return False
            End If

            ' proxy port
            If tbProxyPort.Text Is Nothing OrElse tbProxyPort.Text.Length <= 0 Then
                statusLabel.Text = "Proxy port is a required field when proxy is enabled"
                Return False
            End If

            ' proxy port
            If Me.ProxyPort <= 0 Then
                statusLabel.Text = "Proxy port must be number greater than 0"
                Return False
            End If
        End If

        Return True
    End Function

    ''' <summary>
    ''' Sets the form values according to the config file.
    ''' </summary>
    Private Sub SetFormValues()
        Me.Protocol = CType(Settings.[Default].GetValue("Protocol", GetType(ProtocolType)), ProtocolType)
        Me.HostName = Settings.[Default].GetString("HostName", DefaultHostName)
        Dim defaultPort As Integer
        If Protocol = ProtocolType.Ssh Then
            defaultPort = Ssh.DefaultPort
        Else
            defaultPort = Telnet.DefaultPort
        End If
        Me.Port = Settings.[Default].GetInt32("Port", defaultPort)
        Me.LoginName = Settings.[Default].GetString("Login", DefaultUserName)
        Me.Password = Settings.[Default].GetString("Password", DefaultPassword)
        Me.PrivateKey = Settings.[Default].GetString("PrivateKey")
        Me.ProxyEnabled = Settings.[Default].GetBoolean("ProxyEnabled", False)
        Me.ProxyHostName = Settings.[Default].GetString("ProxyHostName")
        Me.ProxyPort = Settings.[Default].GetInt32("ProxyPort", DefaultProxyPort)
        Me.ProxyType = CType(Settings.[Default].GetValue("ProxyType", GetType(ProxyType)), ProxyType)
        Me.ProxyLoginName = Settings.[Default].GetString("ProxyLogin")
        Me.ProxyPassword = Settings.[Default].GetString("ProxyPassword")
    End Sub

    ''' <summary>
    ''' Save the form values into the config file.
    ''' </summary>
    Private Sub SaveConfig()
        Settings.[Default].SetValue("Protocol", Me.Protocol)
        Settings.[Default].SetValue("HostName", Me.HostName)
        Settings.[Default].SetValue("Port", Me.Port)
        Settings.[Default].SetValue("Login", Me.LoginName)
        Settings.[Default].SetValue("Password", Me.Password)
        Settings.[Default].SetValue("PrivateKey", Me.PrivateKey)
        Settings.[Default].SetValue("ProxyEnabled", Me.ProxyEnabled)
        Settings.[Default].SetValue("ProxyHostName", Me.ProxyHostName)
        Settings.[Default].SetValue("ProxyPort", Me.ProxyPort)
        Settings.[Default].SetValue("ProxyType", Me.ProxyType)
        Settings.[Default].SetValue("ProxyLogin", Me.ProxyLoginName)
        Settings.[Default].SetValue("ProxyPassword", Me.ProxyPassword)

        Settings.[Default].Save()
    End Sub

    ''' <summary>
    ''' Handles the button 'Connect' click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub btnConnect_Click(sender As Object, e As System.EventArgs)
        ' if the form is valid
        If ValidateForm() Then
            ' save the config and submit the form
            _ok = True
            SaveConfig()
            Me.Close()
        End If
    End Sub

    ''' <summary>
    ''' Handles the checkbox 'Proxy' click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cbProxy_Click(sender As Object, e As System.EventArgs)
        ' get the checkbox by casting from sender object
        Dim cb As CheckBox = TryCast(sender, CheckBox)

        ' if sender wasn't checkbox, return
        If cb Is Nothing Then
            Return
        End If

        ' enable / disable Proxy section according to check box state
        If cb.Checked Then
            tbProxy.Enabled = True
            tbProxyPort.Enabled = True
            cbProxy.Checked = True
            cbProxyType.Enabled = True
            tbProxyLogin.Enabled = True
            tbProxyPassword.Enabled = True
        Else
            tbProxy.Enabled = False
            tbProxyPort.Enabled = False
            cbProxy.Checked = False
            cbProxyType.Enabled = False
            tbProxyLogin.Enabled = False
            tbProxyPassword.Enabled = False
        End If
    End Sub

    ''' <summary>
    ''' Handles the button 'PrivateKey' click event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub btnPrivateKey_Click(sender As Object, e As System.EventArgs)
        If openDialog.ShowDialog() = DialogResult.OK Then
            tbPrivateKey.Text = openDialog.FileName
        End If
    End Sub

    ''' <summary>
    ''' Handles 'Protocol' selection change.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub cbProtocol_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim isSsh As Boolean
        If Protocol = ProtocolType.Ssh Then
            isSsh = True
            If Port = Telnet.DefaultPort Then
                Port = Ssh.DefaultPort
            End If
        Else
            isSsh = False
            If Port = Ssh.DefaultPort Then
                Port = Telnet.DefaultPort
            End If
        End If

        tbLogin.Enabled = isSsh Or _enableTelnetLoginInfo
        tbPassword.Enabled = isSsh Or _enableTelnetLoginInfo
        tbPrivateKey.Enabled = isSsh
        btnPrivateKey.Enabled = isSsh
    End Sub

    ''' <summary>
    ''' Handles the form keydown event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub Connection_KeyDown(sender As Object, e As KeyEventArgs)
        ' when 'enter' is pressed the form is submited
        If e.KeyCode = Keys.Enter Then
            If ValidateForm() Then
                _ok = True
                SaveConfig()
                Me.Close()
            End If
        End If

        ' when 'esc' is pressed the form is closed without submiting
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

End Class
