'  
'   Rebex Sample Code License
' 
'   Copyright 2025, 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 Enum ProtocolType
        Ssh = 0
        Telnet = 1
        SerialPort = 2
    End Enum

    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.IContainer = Nothing
    Private statusStrip As System.Windows.Forms.StatusStrip
    Private statusLabel As System.Windows.Forms.ToolStripStatusLabel
    Private openDialog As System.Windows.Forms.OpenFileDialog
    Private flowLayoutPanel1 As FlowLayoutPanel
    Private panel1 As Panel
    Private label0 As Label
    Private cbProtocol As ComboBox
    Private gbPort As GroupBox
    Private gbProxy As GroupBox
    Private tbProxyPassword As TextBox
    Private tbProxyLogin As TextBox
    Private cbProxyType As ComboBox
    Private tbProxy As TextBox
    Private label10 As Label
    Private label9 As Label
    Private label8 As Label
    Private tbProxyPort As TextBox
    Private label6 As Label
    Private cbProxy As CheckBox
    Private label5 As Label
    Private label7 As Label
    Private gbConnection As GroupBox
    Private tbPort As TextBox
    Private label2 As Label
    Private tbHost As TextBox
    Private label1 As Label
    Private btnPrivateKey As Button
    Private tbPrivateKey As TextBox
    Private label11 As Label
    Private tbPassword As TextBox
    Private label4 As Label
    Private tbLogin As TextBox
    Private label3 As Label
    Private btnConnect As Button
    Private tbBaudRate As TextBox
    Private label12 As Label
    Private tbPortName As TextBox
    Private label13 As 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

    ''' <summary>
    ''' Gets the protocol type.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property Protocol() As ProtocolType
        Get
            Return CType(cbProtocol.SelectedIndex, ProtocolType)
        End Get
        Set(value As ProtocolType)
            Dim index As Integer = CInt(value)
            If index >= cbProtocol.Items.Count Then index = 0
            cbProtocol.SelectedIndex = index
        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(value As String)
            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 port__1 As Integer = 0

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

            Return port__1
        End Get
        Set(value As Integer)
            tbPort.Text = value.ToString()
        End Set
    End Property

    ''' <summary>
    ''' Gets the port name.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property PortName() As String
        Get
            Return tbPortName.Text
        End Get
        Set(value As String)
            tbPortName.Text = value
        End Set
    End Property

    ''' <summary>
    ''' Gets the baud rate.
    ''' </summary>
    <Browsable(False)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Property BaudRate() As Integer
        Get
            Dim bRate As Integer = 0

            Try
                bRate = Int32.Parse(tbBaudRate.Text)
            Catch
            End Try

            Return bRate
        End Get
        Set(value As Integer)
            tbBaudRate.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(value As String)
            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(value As String)
            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(value As String)
            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(value As Boolean)
            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(value As String)
            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 pxPort As Integer = 0

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

            Return pxPort
        End Get
        Set(value As Integer)
            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(value As String)
            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(value As String)
            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(value As ProxyType)
            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
        LoadConfig()
    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.statusStrip = New System.Windows.Forms.StatusStrip()
        Me.statusLabel = New System.Windows.Forms.ToolStripStatusLabel()
        Me.openDialog = New System.Windows.Forms.OpenFileDialog()
        Me.flowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
        Me.panel1 = New System.Windows.Forms.Panel()
        Me.label0 = New System.Windows.Forms.Label()
        Me.cbProtocol = New System.Windows.Forms.ComboBox()
        Me.gbPort = New System.Windows.Forms.GroupBox()
        Me.gbProxy = 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.gbConnection = 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.btnConnect = New System.Windows.Forms.Button()
        Me.tbBaudRate = New System.Windows.Forms.TextBox()
        Me.label12 = New System.Windows.Forms.Label()
        Me.tbPortName = New System.Windows.Forms.TextBox()
        Me.label13 = New System.Windows.Forms.Label()
        Me.flowLayoutPanel1.SuspendLayout()
        Me.panel1.SuspendLayout()
        Me.gbPort.SuspendLayout()
        Me.gbProxy.SuspendLayout()
        Me.gbConnection.SuspendLayout()
        Me.statusStrip.SuspendLayout()
        Me.SuspendLayout()
        ' 
        ' statusStrip
        ' 
        Me.statusStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.statusLabel})
        Me.statusStrip.Location = New System.Drawing.Point(0, 375)
        Me.statusStrip.Name = "statusStrip"
        Me.statusStrip.Size = New System.Drawing.Size(351, 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 (*.*)|*.*"
        ' 
        ' flowLayoutPanel1
        ' 
        Me.flowLayoutPanel1.AutoSize = True
        Me.flowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
        Me.flowLayoutPanel1.Controls.Add(Me.panel1)
        Me.flowLayoutPanel1.Controls.Add(Me.gbConnection)
        Me.flowLayoutPanel1.Controls.Add(Me.gbProxy)
        Me.flowLayoutPanel1.Controls.Add(Me.gbPort)
        Me.flowLayoutPanel1.Controls.Add(Me.btnConnect)
        Me.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft
        Me.flowLayoutPanel1.Location = New System.Drawing.Point(0, 0)
        Me.flowLayoutPanel1.MaximumSize = New System.Drawing.Size(351, 0)
        Me.flowLayoutPanel1.MinimumSize = New System.Drawing.Size(351, 0)
        Me.flowLayoutPanel1.Name = "flowLayoutPanel1"
        Me.flowLayoutPanel1.Size = New System.Drawing.Size(351, 375)
        Me.flowLayoutPanel1.TabIndex = 30
        ' 
        ' panel1
        ' 
        Me.panel1.Controls.Add(Me.label0)
        Me.panel1.Controls.Add(Me.cbProtocol)
        Me.panel1.Location = New System.Drawing.Point(6, 3)
        Me.panel1.Name = "panel1"
        Me.panel1.Size = New System.Drawing.Size(342, 35)
        Me.panel1.TabIndex = 0
        ' 
        ' label0
        ' 
        Me.label0.Location = New System.Drawing.Point(6, 12)
        Me.label0.Name = "label0"
        Me.label0.Size = New System.Drawing.Size(81, 18)
        Me.label0.TabIndex = 29
        Me.label0.Text = "Protocol:"
        ' 
        ' cbProtocol
        ' 
        Me.cbProtocol.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cbProtocol.Items.AddRange(New Object() {"SSH", "Telnet", "Serial Port"})
        Me.cbProtocol.Location = New System.Drawing.Point(88, 9)
        Me.cbProtocol.Name = "cbProtocol"
        Me.cbProtocol.Size = New System.Drawing.Size(88, 21)
        Me.cbProtocol.TabIndex = 28
        AddHandler Me.cbProtocol.SelectedIndexChanged, New System.EventHandler(AddressOf Me.cbProtocol_SelectedIndexChanged)
        ' 
        ' gbPort
        ' 
        Me.gbPort.Controls.Add(Me.tbBaudRate)
        Me.gbPort.Controls.Add(Me.label12)
        Me.gbPort.Controls.Add(Me.tbPortName)
        Me.gbPort.Controls.Add(Me.label13)
        Me.gbPort.Location = New System.Drawing.Point(6, 288)
        Me.gbPort.Name = "gbPort"
        Me.gbPort.Size = New System.Drawing.Size(342, 50)
        Me.gbPort.TabIndex = 1
        Me.gbPort.TabStop = False
        Me.gbPort.Text = "Port settings"
        ' 
        ' gbProxy
        ' 
        Me.gbProxy.Controls.Add(Me.tbProxyPassword)
        Me.gbProxy.Controls.Add(Me.tbProxyLogin)
        Me.gbProxy.Controls.Add(Me.cbProxyType)
        Me.gbProxy.Controls.Add(Me.tbProxy)
        Me.gbProxy.Controls.Add(Me.label10)
        Me.gbProxy.Controls.Add(Me.label9)
        Me.gbProxy.Controls.Add(Me.label8)
        Me.gbProxy.Controls.Add(Me.tbProxyPort)
        Me.gbProxy.Controls.Add(Me.label6)
        Me.gbProxy.Controls.Add(Me.cbProxy)
        Me.gbProxy.Controls.Add(Me.label5)
        Me.gbProxy.Controls.Add(Me.label7)
        Me.gbProxy.Location = New System.Drawing.Point(6, 154)
        Me.gbProxy.Name = "gbProxy"
        Me.gbProxy.Size = New System.Drawing.Size(342, 128)
        Me.gbProxy.TabIndex = 3
        Me.gbProxy.TabStop = False
        Me.gbProxy.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:"
        ' 
        ' gbConnection
        ' 
        Me.gbConnection.Controls.Add(Me.tbPort)
        Me.gbConnection.Controls.Add(Me.label2)
        Me.gbConnection.Controls.Add(Me.tbHost)
        Me.gbConnection.Controls.Add(Me.label1)
        Me.gbConnection.Controls.Add(Me.btnPrivateKey)
        Me.gbConnection.Controls.Add(Me.tbPrivateKey)
        Me.gbConnection.Controls.Add(Me.label11)
        Me.gbConnection.Controls.Add(Me.tbPassword)
        Me.gbConnection.Controls.Add(Me.label4)
        Me.gbConnection.Controls.Add(Me.tbLogin)
        Me.gbConnection.Controls.Add(Me.label3)
        Me.gbConnection.Location = New System.Drawing.Point(6, 44)
        Me.gbConnection.Name = "gbConnection"
        Me.gbConnection.Size = New System.Drawing.Size(342, 104)
        Me.gbConnection.TabIndex = 2
        Me.gbConnection.TabStop = False
        Me.gbConnection.Text = "Connection settings"
        ' 
        ' tbPort
        ' 
        Me.tbPort.Location = New System.Drawing.Point(248, 24)
        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, 24)
        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, 24)
        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, 24)
        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, 72)
        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, 72)
        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, 72)
        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, 48)
        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, 48)
        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, 48)
        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, 48)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(81, 23)
        Me.label3.TabIndex = 14
        Me.label3.Text = "Login name:"
        ' 
        ' btnConnect
        ' 
        Me.btnConnect.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.btnConnect.Location = New System.Drawing.Point(275, 344)
        Me.btnConnect.Name = "btnConnect"
        Me.btnConnect.Size = New System.Drawing.Size(73, 23)
        Me.btnConnect.TabIndex = 4
        Me.btnConnect.Text = "Connect"
        AddHandler Me.btnConnect.Click, New System.EventHandler(AddressOf Me.btnConnect_Click)
        ' 
        ' tbBaudRate
        ' 
        Me.tbBaudRate.Location = New System.Drawing.Point(248, 19)
        Me.tbBaudRate.Name = "tbBaudRate"
        Me.tbBaudRate.Size = New System.Drawing.Size(88, 20)
        Me.tbBaudRate.TabIndex = 17
        Me.tbBaudRate.Text = "9600"
        ' 
        ' label12
        ' 
        Me.label12.Location = New System.Drawing.Point(184, 19)
        Me.label12.Name = "label12"
        Me.label12.Size = New System.Drawing.Size(64, 23)
        Me.label12.TabIndex = 16
        Me.label12.Text = "Baud rate:"
        ' 
        ' tbPortName
        ' 
        Me.tbPortName.Location = New System.Drawing.Point(88, 19)
        Me.tbPortName.Name = "tbPortName"
        Me.tbPortName.Size = New System.Drawing.Size(88, 20)
        Me.tbPortName.TabIndex = 15
        ' 
        ' label13
        ' 
        Me.label13.Location = New System.Drawing.Point(7, 19)
        Me.label13.Name = "label13"
        Me.label13.Size = New System.Drawing.Size(81, 23)
        Me.label13.TabIndex = 14
        Me.label13.Text = "Port name:"
        ' 
        ' Connection
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoSize = True
        Me.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
        Me.ClientSize = New System.Drawing.Size(351, 397)
        Me.Controls.Add(Me.flowLayoutPanel1)
        Me.Controls.Add(Me.statusStrip)
        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.flowLayoutPanel1.ResumeLayout(False)
        Me.panel1.ResumeLayout(False)
        Me.gbPort.ResumeLayout(False)
        Me.gbPort.PerformLayout()
        Me.gbProxy.ResumeLayout(False)
        Me.gbProxy.PerformLayout()
        Me.gbConnection.ResumeLayout(False)
        Me.gbConnection.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
        If Protocol = ProtocolType.SerialPort Then
            ' port
            If tbPortName.Text Is Nothing OrElse tbPortName.Text.Length <= 0 Then
                statusLabel.Text = "Port name is a required field"
                Return False
            End If

            ' baud rate
            If Me.BaudRate <= 0 Then
                statusLabel.Text = "Baud rate must be greater than 0"
                Return False
            End If
        Else
            ' 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
        End If

        Return True
    End Function

    ''' <summary>
    ''' Sets the form values according to the config file.
    ''' </summary>
    Private Sub LoadConfig()
        Me.Protocol = CType(Settings.[Default].GetValue("Protocol", GetType(ProtocolType)), ProtocolType)
        Me.HostName = Settings.[Default].GetString("HostName")
        Me.Port = Settings.[Default].GetInt32("Port", IIf(Me.Protocol = ProtocolType.Ssh, Ssh.DefaultPort, Telnet.DefaultPort))
        Me.LoginName = Settings.[Default].GetString("Login")
        Me.Password = Settings.[Default].GetString("Password")
        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")
        Me.PortName = Settings.[Default].GetString("PortName")
        Me.BaudRate = Settings.[Default].GetInt32("BaudRate", 9600)
    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].SetValue("PortName", Me.PortName)
        Settings.[Default].SetValue("BaudRate", Me.BaudRate)

        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 = False
        Dim isSerial As Boolean = False
        Select Case Protocol
            Case ProtocolType.Ssh
                isSsh = True
                If Port = Telnet.DefaultPort Then
                    Port = Ssh.DefaultPort
                End If
                Exit Select
            Case ProtocolType.Telnet
                If Port = Ssh.DefaultPort Then
                    Port = Telnet.DefaultPort
                End If
                Exit Select
            Case ProtocolType.SerialPort
                isSerial = True
                Exit Select
        End Select

        tbLogin.Enabled = isSsh
        tbPassword.Enabled = isSsh
        tbPrivateKey.Enabled = isSsh
        btnPrivateKey.Enabled = isSsh

        gbPort.Visible = isSerial
        gbConnection.Visible = Not isSerial
        gbProxy.Visible = Not isSerial
    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
