Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Data.OleDb

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private ClientAccesses As ArrayList
    Private SocketListener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    Private Time_ClientAccesses(0) As DateTime
    Private DispDataBuf As String
    Private DispDataBuf2 As String
    Private DispDataBuf3 As String
    Private DispDataBuf4 As String
    Private DispDataBufS As String
    Private DispDataBufS2 As String
    Private DispDataBufS3 As String
    Private DispDataBufS4 As String
    Private DispDataBufSQL As String
    Private DispDataBufERR As String

    Private _PortNumber As Integer
    Private _TimeOut As Integer = 60000 '60秒以上アクセスがない場合は切断されたとみなす
    Private _TimeOutCheckInterval As Integer = 120000  '120秒おきに切断を確認
    Private _TimeOutCheckType As String = "NoCheck" '"Time" "DataSend" "NoCheck"
    Private _CheckTimeOutData As Byte() = Encoding.Default.GetBytes(" ")
    Private _NICNo As Integer = 0
    Private _buffer_size As Integer = 65536

    Private statebuff As New StateObject()
    Private start_flg As Boolean
    Private button_flg As Boolean
    Private WithSvrServerAddress As String
    Private WithSvrPortNumber As Integer

    Private customize_flg As Boolean

    Private SQLPrxyStartObj As New Object

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents txtPort As System.Windows.Forms.TextBox
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents ComboIP As System.Windows.Forms.ComboBox
    Friend WithEvents btnClear As System.Windows.Forms.Button
    Friend WithEvents btnSendToClient As System.Windows.Forms.Button
    Friend WithEvents txtSQLPort As System.Windows.Forms.TextBox
    Friend WithEvents btnSendToSQL As System.Windows.Forms.Button
    Friend WithEvents btnSQLClear As System.Windows.Forms.Button
    Friend WithEvents txtList As System.Windows.Forms.TextBox
    Friend WithEvents txtList2 As System.Windows.Forms.TextBox
    Friend WithEvents txtSQLList As System.Windows.Forms.TextBox
    Friend WithEvents txtSQLList2 As System.Windows.Forms.TextBox
    Friend WithEvents txtIP As System.Windows.Forms.TextBox
    Friend WithEvents chkStart As System.Windows.Forms.CheckBox
    Friend WithEvents label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents txtSQL As System.Windows.Forms.TextBox
    Friend WithEvents chkProc As System.Windows.Forms.CheckBox
    Friend WithEvents txtListToClient As System.Windows.Forms.TextBox
    Friend WithEvents txtListToClient2 As System.Windows.Forms.TextBox
    Friend WithEvents txtSQLListToServer As System.Windows.Forms.TextBox
    Friend WithEvents txtSQLListToServer2 As System.Windows.Forms.TextBox
    Friend WithEvents Label11 As System.Windows.Forms.Label
    Friend WithEvents Label12 As System.Windows.Forms.Label
    Friend WithEvents Label13 As System.Windows.Forms.Label
    Friend WithEvents btnClear2 As System.Windows.Forms.Button
    Friend WithEvents btnClear3 As System.Windows.Forms.Button
    Friend WithEvents btnClearAll As System.Windows.Forms.Button
    Friend WithEvents txtErr As System.Windows.Forms.TextBox
    Friend WithEvents Label16 As System.Windows.Forms.Label
    Friend WithEvents Label18 As System.Windows.Forms.Label
    Friend WithEvents Label19 As System.Windows.Forms.Label
    Friend WithEvents Label20 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.btnStart = New System.Windows.Forms.Button
        Me.txtPort = New System.Windows.Forms.TextBox
        Me.label1 = New System.Windows.Forms.Label
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Label4 = New System.Windows.Forms.Label
        Me.ComboIP = New System.Windows.Forms.ComboBox
        Me.btnClear = New System.Windows.Forms.Button
        Me.txtSQLPort = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.btnSendToSQL = New System.Windows.Forms.Button
        Me.btnSQLClear = New System.Windows.Forms.Button
        Me.btnSendToClient = New System.Windows.Forms.Button
        Me.txtList = New System.Windows.Forms.TextBox
        Me.txtList2 = New System.Windows.Forms.TextBox
        Me.txtSQLList = New System.Windows.Forms.TextBox
        Me.txtSQLList2 = New System.Windows.Forms.TextBox
        Me.txtIP = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.chkStart = New System.Windows.Forms.CheckBox
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.Label7 = New System.Windows.Forms.Label
        Me.Label8 = New System.Windows.Forms.Label
        Me.Label9 = New System.Windows.Forms.Label
        Me.Label10 = New System.Windows.Forms.Label
        Me.txtSQL = New System.Windows.Forms.TextBox
        Me.chkProc = New System.Windows.Forms.CheckBox
        Me.txtListToClient = New System.Windows.Forms.TextBox
        Me.txtListToClient2 = New System.Windows.Forms.TextBox
        Me.txtSQLListToServer = New System.Windows.Forms.TextBox
        Me.txtSQLListToServer2 = New System.Windows.Forms.TextBox
        Me.Label11 = New System.Windows.Forms.Label
        Me.Label12 = New System.Windows.Forms.Label
        Me.Label13 = New System.Windows.Forms.Label
        Me.btnClear2 = New System.Windows.Forms.Button
        Me.btnClear3 = New System.Windows.Forms.Button
        Me.btnClearAll = New System.Windows.Forms.Button
        Me.txtErr = New System.Windows.Forms.TextBox
        Me.Label16 = New System.Windows.Forms.Label
        Me.Label18 = New System.Windows.Forms.Label
        Me.Label19 = New System.Windows.Forms.Label
        Me.Label20 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(17, 1)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(72, 25)
        Me.btnStart.TabIndex = 0
        Me.btnStart.Text = "Start"
        '
        'txtPort
        '
        Me.txtPort.Location = New System.Drawing.Point(222, 61)
        Me.txtPort.Name = "txtPort"
        Me.txtPort.Size = New System.Drawing.Size(39, 19)
        Me.txtPort.TabIndex = 6
        Me.txtPort.Text = "1499"
        '
        'label1
        '
        Me.label1.Location = New System.Drawing.Point(99, 67)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(126, 13)
        Me.label1.TabIndex = 7
        Me.label1.Text = "このサーバーのポート番号"
        '
        'Timer1
        '
        Me.Timer1.Interval = 120000
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(19, 92)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(155, 13)
        Me.Label4.TabIndex = 21
        Me.Label4.Text = "ネットワークデバイスのIPアドレス"
        '
        'ComboIP
        '
        Me.ComboIP.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.ComboIP.Location = New System.Drawing.Point(169, 85)
        Me.ComboIP.Name = "ComboIP"
        Me.ComboIP.Size = New System.Drawing.Size(92, 20)
        Me.ComboIP.TabIndex = 20
        '
        'btnClear
        '
        Me.btnClear.Location = New System.Drawing.Point(189, 236)
        Me.btnClear.Name = "btnClear"
        Me.btnClear.Size = New System.Drawing.Size(73, 21)
        Me.btnClear.TabIndex = 23
        Me.btnClear.Text = "表示のクリア"
        Me.btnClear.UseVisualStyleBackColor = True
        '
        'txtSQLPort
        '
        Me.txtSQLPort.Enabled = False
        Me.txtSQLPort.Location = New System.Drawing.Point(444, 80)
        Me.txtSQLPort.Name = "txtSQLPort"
        Me.txtSQLPort.Size = New System.Drawing.Size(40, 19)
        Me.txtSQLPort.TabIndex = 27
        Me.txtSQLPort.Text = "1433"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(314, 86)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(199, 16)
        Me.Label2.TabIndex = 28
        Me.Label2.Text = "SQL Serverのポート番号"
        '
        'btnSendToSQL
        '
        Me.btnSendToSQL.Location = New System.Drawing.Point(18, 236)
        Me.btnSendToSQL.Name = "btnSendToSQL"
        Me.btnSendToSQL.Size = New System.Drawing.Size(165, 31)
        Me.btnSendToSQL.TabIndex = 29
        Me.btnSendToSQL.Text = "上のデータをSQLServerへ送信"
        '
        'btnSQLClear
        '
        Me.btnSQLClear.Location = New System.Drawing.Point(488, 239)
        Me.btnSQLClear.Name = "btnSQLClear"
        Me.btnSQLClear.Size = New System.Drawing.Size(75, 20)
        Me.btnSQLClear.TabIndex = 31
        Me.btnSQLClear.Text = "表示のクリア"
        Me.btnSQLClear.UseVisualStyleBackColor = True
        '
        'btnSendToClient
        '
        Me.btnSendToClient.Location = New System.Drawing.Point(316, 237)
        Me.btnSendToClient.Name = "btnSendToClient"
        Me.btnSendToClient.Size = New System.Drawing.Size(166, 30)
        Me.btnSendToClient.TabIndex = 32
        Me.btnSendToClient.Text = "上のデータをクライアントへ送信"
        '
        'txtList
        '
        Me.txtList.Location = New System.Drawing.Point(18, 126)
        Me.txtList.Multiline = True
        Me.txtList.Name = "txtList"
        Me.txtList.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtList.Size = New System.Drawing.Size(156, 104)
        Me.txtList.TabIndex = 33
        '
        'txtList2
        '
        Me.txtList2.Location = New System.Drawing.Point(180, 126)
        Me.txtList2.Multiline = True
        Me.txtList2.Name = "txtList2"
        Me.txtList2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtList2.Size = New System.Drawing.Size(81, 104)
        Me.txtList2.TabIndex = 34
        '
        'txtSQLList
        '
        Me.txtSQLList.Enabled = False
        Me.txtSQLList.Location = New System.Drawing.Point(316, 127)
        Me.txtSQLList.Multiline = True
        Me.txtSQLList.Name = "txtSQLList"
        Me.txtSQLList.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSQLList.Size = New System.Drawing.Size(157, 106)
        Me.txtSQLList.TabIndex = 35
        '
        'txtSQLList2
        '
        Me.txtSQLList2.Enabled = False
        Me.txtSQLList2.Location = New System.Drawing.Point(481, 127)
        Me.txtSQLList2.Multiline = True
        Me.txtSQLList2.Name = "txtSQLList2"
        Me.txtSQLList2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSQLList2.Size = New System.Drawing.Size(82, 106)
        Me.txtSQLList2.TabIndex = 36
        '
        'txtIP
        '
        Me.txtIP.Enabled = False
        Me.txtIP.Location = New System.Drawing.Point(444, 61)
        Me.txtIP.Name = "txtIP"
        Me.txtIP.Size = New System.Drawing.Size(97, 19)
        Me.txtIP.TabIndex = 37
        Me.txtIP.Text = "localhost"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(314, 64)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(128, 13)
        Me.Label3.TabIndex = 38
        Me.Label3.Text = "SQL ServerのIPアドレス"
        '
        'chkStart
        '
        Me.chkStart.AutoSize = True
        Me.chkStart.Location = New System.Drawing.Point(95, 1)
        Me.chkStart.Name = "chkStart"
        Me.chkStart.Size = New System.Drawing.Size(195, 16)
        Me.chkStart.TabIndex = 39
        Me.chkStart.Text = "データ受信時に自動的に送信しない"
        Me.chkStart.UseVisualStyleBackColor = True
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label5.Location = New System.Drawing.Point(16, 111)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(169, 12)
        Me.Label5.TabIndex = 40
        Me.Label5.Text = "1, クライアントから受信した内容"
        '
        'Label6
        '
        Me.Label6.AutoSize = True
        Me.Label6.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label6.Location = New System.Drawing.Point(315, 111)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(180, 12)
        Me.Label6.TabIndex = 41
        Me.Label6.Text = "3, SQL Serverから受信した内容"
        '
        'Label7
        '
        Me.Label7.AutoSize = True
        Me.Label7.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label7.Location = New System.Drawing.Point(19, 32)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(244, 16)
        Me.Label7.TabIndex = 42
        Me.Label7.Text = "クライアントとやりとりするサーバー部"
        '
        'Label8
        '
        Me.Label8.AutoSize = True
        Me.Label8.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label8.Location = New System.Drawing.Point(298, 32)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(284, 16)
        Me.Label8.TabIndex = 43
        Me.Label8.Text = "SQL Serverとやり取りするクライアント部"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(3, 32)
        Me.Label9.MaximumSize = New System.Drawing.Size(0, 1)
        Me.Label9.MinimumSize = New System.Drawing.Size(0, 1)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(0, 1)
        Me.Label9.TabIndex = 44
        '
        'Label10
        '
        Me.Label10.AutoSize = True
        Me.Label10.Location = New System.Drawing.Point(3, 29)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(0, 12)
        Me.Label10.TabIndex = 45
        '
        'txtSQL
        '
        Me.txtSQL.Location = New System.Drawing.Point(84, 419)
        Me.txtSQL.Multiline = True
        Me.txtSQL.Name = "txtSQL"
        Me.txtSQL.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSQL.Size = New System.Drawing.Size(476, 30)
        Me.txtSQL.TabIndex = 46
        '
        'chkProc
        '
        Me.chkProc.AutoSize = True
        Me.chkProc.Location = New System.Drawing.Point(23, 376)
        Me.chkProc.Name = "chkProc"
        Me.chkProc.Size = New System.Drawing.Size(182, 16)
        Me.chkProc.TabIndex = 47
        Me.chkProc.Text = "カスタマイズした機能を使用しない"
        Me.chkProc.UseVisualStyleBackColor = True
        '
        'txtListToClient
        '
        Me.txtListToClient.Location = New System.Drawing.Point(17, 287)
        Me.txtListToClient.Multiline = True
        Me.txtListToClient.Name = "txtListToClient"
        Me.txtListToClient.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtListToClient.Size = New System.Drawing.Size(156, 56)
        Me.txtListToClient.TabIndex = 48
        '
        'txtListToClient2
        '
        Me.txtListToClient2.Location = New System.Drawing.Point(179, 287)
        Me.txtListToClient2.Multiline = True
        Me.txtListToClient2.Name = "txtListToClient2"
        Me.txtListToClient2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtListToClient2.Size = New System.Drawing.Size(81, 56)
        Me.txtListToClient2.TabIndex = 49
        '
        'txtSQLListToServer
        '
        Me.txtSQLListToServer.Enabled = False
        Me.txtSQLListToServer.Location = New System.Drawing.Point(315, 285)
        Me.txtSQLListToServer.Multiline = True
        Me.txtSQLListToServer.Name = "txtSQLListToServer"
        Me.txtSQLListToServer.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSQLListToServer.Size = New System.Drawing.Size(157, 58)
        Me.txtSQLListToServer.TabIndex = 50
        '
        'txtSQLListToServer2
        '
        Me.txtSQLListToServer2.Enabled = False
        Me.txtSQLListToServer2.Location = New System.Drawing.Point(481, 283)
        Me.txtSQLListToServer2.Multiline = True
        Me.txtSQLListToServer2.Name = "txtSQLListToServer2"
        Me.txtSQLListToServer2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSQLListToServer2.Size = New System.Drawing.Size(82, 60)
        Me.txtSQLListToServer2.TabIndex = 51
        '
        'Label11
        '
        Me.Label11.AutoSize = True
        Me.Label11.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label11.Location = New System.Drawing.Point(18, 273)
        Me.Label11.Name = "Label11"
        Me.Label11.Size = New System.Drawing.Size(160, 12)
        Me.Label11.TabIndex = 52
        Me.Label11.Text = "4, クライアントへ送信した内容"
        '
        'Label12
        '
        Me.Label12.AutoSize = True
        Me.Label12.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label12.Location = New System.Drawing.Point(314, 270)
        Me.Label12.Name = "Label12"
        Me.Label12.Size = New System.Drawing.Size(171, 12)
        Me.Label12.TabIndex = 53
        Me.Label12.Text = "2, SQL Serverへ送信した内容"
        '
        'Label13
        '
        Me.Label13.AutoSize = True
        Me.Label13.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label13.Location = New System.Drawing.Point(16, 419)
        Me.Label13.Name = "Label13"
        Me.Label13.Size = New System.Drawing.Size(62, 24)
        Me.Label13.TabIndex = 54
        Me.Label13.Text = "抽出された" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "SQL文"
        '
        'btnClear2
        '
        Me.btnClear2.Location = New System.Drawing.Point(188, 349)
        Me.btnClear2.Name = "btnClear2"
        Me.btnClear2.Size = New System.Drawing.Size(73, 21)
        Me.btnClear2.TabIndex = 55
        Me.btnClear2.Text = "表示のクリア"
        Me.btnClear2.UseVisualStyleBackColor = True
        '
        'btnClear3
        '
        Me.btnClear3.Enabled = False
        Me.btnClear3.Location = New System.Drawing.Point(482, 349)
        Me.btnClear3.Name = "btnClear3"
        Me.btnClear3.Size = New System.Drawing.Size(74, 21)
        Me.btnClear3.TabIndex = 56
        Me.btnClear3.Text = "表示のクリア"
        Me.btnClear3.UseVisualStyleBackColor = True
        '
        'btnClearAll
        '
        Me.btnClearAll.Location = New System.Drawing.Point(444, 1)
        Me.btnClearAll.Name = "btnClearAll"
        Me.btnClearAll.Size = New System.Drawing.Size(115, 25)
        Me.btnClearAll.TabIndex = 57
        Me.btnClearAll.Text = "すべての表示のクリア"
        Me.btnClearAll.UseVisualStyleBackColor = True
        '
        'txtErr
        '
        Me.txtErr.Location = New System.Drawing.Point(84, 452)
        Me.txtErr.Multiline = True
        Me.txtErr.Name = "txtErr"
        Me.txtErr.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtErr.Size = New System.Drawing.Size(476, 27)
        Me.txtErr.TabIndex = 62
        '
        'Label16
        '
        Me.Label16.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label16.Location = New System.Drawing.Point(16, 455)
        Me.Label16.Name = "Label16"
        Me.Label16.Size = New System.Drawing.Size(62, 24)
        Me.Label16.TabIndex = 63
        Me.Label16.Text = "ADO.NETエラー"
        '
        'Label18
        '
        Me.Label18.AutoSize = True
        Me.Label18.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label18.Location = New System.Drawing.Point(15, 395)
        Me.Label18.Name = "Label18"
        Me.Label18.Size = New System.Drawing.Size(102, 16)
        Me.Label18.TabIndex = 65
        Me.Label18.Text = "カスタマイズ部"
        '
        'Label19
        '
        Me.Label19.AutoSize = True
        Me.Label19.Location = New System.Drawing.Point(123, 399)
        Me.Label19.Name = "Label19"
        Me.Label19.Size = New System.Drawing.Size(304, 12)
        Me.Label19.TabIndex = 66
        Me.Label19.Text = "-----SQLをJETデータベースエンジンに転送して処理する-----"
        '
        'Label20
        '
        Me.Label20.AutoSize = True
        Me.Label20.Location = New System.Drawing.Point(284, 29)
        Me.Label20.Name = "Label20"
        Me.Label20.Size = New System.Drawing.Size(8, 348)
        Me.Label20.TabIndex = 67
        Me.Label20.Text = "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & _
            "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "|" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10)
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(588, 480)
        Me.Controls.Add(Me.Label20)
        Me.Controls.Add(Me.Label19)
        Me.Controls.Add(Me.Label18)
        Me.Controls.Add(Me.Label16)
        Me.Controls.Add(Me.txtErr)
        Me.Controls.Add(Me.btnClearAll)
        Me.Controls.Add(Me.btnClear3)
        Me.Controls.Add(Me.btnClear2)
        Me.Controls.Add(Me.Label13)
        Me.Controls.Add(Me.Label12)
        Me.Controls.Add(Me.Label11)
        Me.Controls.Add(Me.txtSQLListToServer2)
        Me.Controls.Add(Me.txtSQLListToServer)
        Me.Controls.Add(Me.txtListToClient2)
        Me.Controls.Add(Me.txtListToClient)
        Me.Controls.Add(Me.chkProc)
        Me.Controls.Add(Me.txtSQL)
        Me.Controls.Add(Me.Label10)
        Me.Controls.Add(Me.Label9)
        Me.Controls.Add(Me.btnSQLClear)
        Me.Controls.Add(Me.Label8)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.chkStart)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.txtIP)
        Me.Controls.Add(Me.txtSQLList2)
        Me.Controls.Add(Me.txtSQLList)
        Me.Controls.Add(Me.txtList2)
        Me.Controls.Add(Me.txtList)
        Me.Controls.Add(Me.ComboIP)
        Me.Controls.Add(Me.btnSendToClient)
        Me.Controls.Add(Me.btnSendToSQL)
        Me.Controls.Add(Me.txtSQLPort)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.btnClear)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.txtPort)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.btnStart)
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "SQLプロキシ"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region
    Private Delegate Sub AsyncServerDelegate()
    Private Function AppSQLPrxyStart() As Object
        'カスタマイズしない場合は、次の1行を残す
        'AppSQLPrxyStart = Nothing
        Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & GetPathName() & "pubs.mdb"
        'SQL Serverを使用する場合は次の接続文字列を使用する。
        'Dim ConnectionString As String = "Provider=SQLOLEDB;Server=localhost;Connect Timeout=6000;Database=pubs;user id=sa;password=")
        Dim DBCon As New OleDbConnection
        DBCon.ConnectionString = ConnectionString
        DBCon.Open()
        AppSQLPrxyStart = DBCon
    End Function
    Private Function AppSQLPrxyReceiveFromClient(ByVal SQLstring As String, ByRef state As StateObject) As Byte()
        'カスタマイズしない場合は、次の1行を残す
        'AppSQLPrxyReceiveFromClient = state.stateclient.send_buffer
        Dim byteData As Byte() = state.stateclient.send_buffer
        Dim byteSendData As Byte()
        Dim row As DataRow
        Dim column As DataColumn
        Dim i, j As Integer
        Dim datalen As Integer
        Dim dimcount As Integer = -1

        AppSQLPrxyReceiveFromClient = Nothing
        _OutputDataSQL(SQLstring)

        Select Case CStr(Hex(byteData(0)))
            Case "12" 'connect
                Dim data12 As String = "04 01 00 25 00 00 01 00 00 00 15 00 06 01 00 1B " _
                                    & "00 01 02 00 1C 00 01 03 00 1D 00 00 FF 08 00 02 " _
                                    & "FE 00 00 02 00 "
                ReDim byteSendData(data12.Length / 3 - 1)
                j = 0
                For i = 1 To data12.Length - 1 Step 3
                    byteSendData(j) = CType("&H" & Mid(data12, i, 2), Byte)
                    j = j + 1
                Next
                dimcount = j - 1
                state.send_buffer = byteSendData
                SendToClient(state) '*******クライアントへ送信********

            Case "10"
                Dim data10 As String = "04 01 01 25 00 33 01 00 E3 17 00 01 04 70 00 75 " _
                                     & "00 62 00 73 00 06 6D 00 61 00 73 00 74 00 65 00 " _
                                     & "72 00 AB 54 00 45 16 00 00 02 00 1F 00 C7 30 FC " _
                                     & "30 BF 30 D9 30 FC 30 B9 30 20 00 B3 30 F3 30 C6 " _
                                     & "30 AD 30 B9 30 C8 30 6F 30 20 00 27 00 70 00 75 " _
                                     & "00 62 00 73 00 27 00 20 00 6B 30 09 59 F4 66 55 " _
                                     & "30 8C 30 7E 30 57 30 5F 30 02 30 05 50 00 41 00 " _
                                     & "52 00 49 00 53 00 00 00 00 E3 08 00 07 05 11 04 " _
                                     & "D0 00 00 00 E3 09 00 02 03 E5 65 2C 67 9E 8A 00 " _
                                     & "AB 3A 00 47 16 00 00 01 00 12 00 00 8A 9E 8A 2D " _
                                     & "8A 9A 5B 92 30 20 00 E5 65 2C 67 9E 8A 20 00 6B " _
                                     & "30 09 59 F4 66 57 30 7E 30 57 30 5F 30 02 30 05 " _
                                     & "50 00 41 00 52 00 49 00 53 00 00 00 00 AD 36 00 " _
                                     & "01 71 00 00 01 16 4D 00 69 00 63 00 72 00 6F 00 " _
                                     & "73 00 6F 00 66 00 74 00 20 00 53 00 51 00 4C 00 " _
                                     & "20 00 53 00 65 00 72 00 76 00 65 00 72 00 00 00 " _
                                     & "00 00 08 00 02 F8 E3 13 00 04 04 38 00 30 00 30 " _
                                     & "00 30 00 04 34 00 30 00 39 00 36 00 FD 00 00 00 " _
                                     & "00 00 00 00 00 "

                ReDim byteSendData(data10.Length / 3 - 1)
                j = 0
                For i = 1 To data10.Length - 1 Step 3
                    byteSendData(j) = CType("&H" & Mid(data10, i, 2), Byte)
                    j = j + 1
                Next
                dimcount = j - 1
                state.send_buffer = byteSendData
                SendToClient(state) '*******クライアントへ送信********


            Case "1"

                Dim command As OleDbCommand = New OleDbCommand(Trim(SQLstring), SQLPrxyStartObj)

                If InStr(UCase(Trim(SQLstring)), "SELECT") > 0 Then
                    Dim SAdapter As OleDbDataAdapter = New OleDbDataAdapter()
                    SAdapter.SelectCommand = command
                    '            Dim DS As DataSet = New DataSet("sql")    'DS.Tables(0)
                    '            SAdapter.Fill(DS)
                    Dim commandBuilder As New OleDbCommandBuilder(SAdapter)
                    Dim table As New DataTable()
                    'table.Locale = System.Globalization.CultureInfo.InvariantCulture
                    Try
                        SAdapter.Fill(table)
                    Catch ex As OleDbException
                        _OutputDataERR(ex.Message)
                        Exit Function
                    End Try

                    ReDim byteSendData(_buffer_size)

                    '*******メッセージヘッダー作成********
                    byteSendData(0) = CType("&H04", Byte)
                    byteSendData(1) = CType("&H01", Byte)
                    byteSendData(2) = 0 '2,3バイト目でレコード長
                    byteSendData(3) = 0 '2,3バイト目でレコード長
                    byteSendData(4) = 0
                    byteSendData(5) = CType("&H33", Byte)
                    byteSendData(6) = CType("&H01", Byte)
                    byteSendData(7) = 0
                    byteSendData(8) = CType("&H81", Byte)
                    byteSendData(9) = CType(table.Columns.Count, Byte) 'カラム数
                    byteSendData(10) = 0

                    dimcount = 11
                    i = 0
                    For Each column In table.Columns
                        i = i + 1
                        '*******カラムネームヘッダー作成********
                        byteSendData(dimcount) = 0     'CType("&H01", Byte) のときもある
                        byteSendData(dimcount + 1) = 0 'CType("&H01", Byte) のときもある
                        byteSendData(dimcount + 2) = CType("&H08", Byte)
                        byteSendData(dimcount + 3) = 0
                        byteSendData(dimcount + 4) = CType("&HA7", Byte)
                        byteSendData(dimcount + 5) = CType("&H14", Byte) '&H28 &H0B のときもある
                        byteSendData(dimcount + 6) = 0
                        byteSendData(dimcount + 7) = CType("&H11", Byte)
                        byteSendData(dimcount + 8) = CType("&H04", Byte)
                        byteSendData(dimcount + 9) = CType("&HD0", Byte)
                        byteSendData(dimcount + 10) = 0
                        byteSendData(dimcount + 11) = 0
                        byteSendData(dimcount + 12) = CType(column.Caption.Length, Byte) 'カラム名の長さ
                        dimcount = dimcount + 13
                        '*******カラムネームデータ作成********
                        For j = 1 To column.Caption.Length
                            byteSendData(dimcount) = CType(CStr(Asc(Mid(column.Caption, j, 1))), Byte) 'au_fname
                            byteSendData(dimcount + 1) = 0
                            dimcount = dimcount + 2
                        Next
                    Next
                    '*******ここからデータ
                    byteSendData(dimcount) = CType("&HD1", Byte) '92
                    dimcount = dimcount + 1
                    datalen = 0
                    For Each row In table.Rows
                        For Each column In table.Columns
                            '*******カラムヘッダー********
                            datalen = datalen + Len(row(column))
                            byteSendData(dimcount) = CType(Len(row(column)), Byte) 'データの長さ
                            byteSendData(dimcount + 1) = 0
                            '*******カラムデータ*******
                            dimcount = dimcount + 1
                            If dimcount + 1 > UBound(byteSendData) Then 'byteSendDataの配列の大きさが足りなくなったら再割り当て
                                ReDim Preserve byteSendData(dimcount + Len(row(column)) + 10)
                            End If
                            For i = 1 To Len(row(column))
                                byteSendData(dimcount + i) = CType(CStr(Asc(Mid(row(column), i, 1))), Byte)
                            Next
                            dimcount = dimcount + i
                        Next column
                        byteSendData(dimcount) = CType("&HD1", Byte)
                        dimcount = dimcount + 1
                    Next row
                    If CStr(Hex(byteSendData(dimcount - 1))) = "D1" Then
                        dimcount = dimcount - 1
                    End If

                    '*******フッター作成********
                    Dim datafoot As String = "FD 10 00 C1 00 01 00 00 00 "
                    j = dimcount
                    For i = 1 To datafoot.Length - 1 Step 3
                        byteSendData(j) = CType("&H" & Mid(datafoot, i, 2), Byte)
                        j = j + 1
                    Next
                    dimcount = j - 1

                    '*******全レコード長*********
                    If dimcount < 255 Then
                        byteSendData(3) = CType(dimcount + 1, Byte) '全レコード長
                    Else
                        Dim int1, int2 As Integer
                        int1 = (dimcount + 1) \ 256
                        int2 = (dimcount + 1) Mod 256
                        byteSendData(2) = CType(int1, Byte) '全レコード長
                        byteSendData(3) = CType(int2, Byte) '全レコード長
                    End If

                    ReDim Preserve byteSendData(dimcount)
                    state.send_buffer = byteSendData
                    SendToClient(state) '*******クライアントへ送信********
                End If
                If InStr(UCase(Trim(SQLstring)), "INSERT") > 0 Or InStr(UCase(Trim(SQLstring)), "UPDATE") > 0 Then
                    Try
                        command.ExecuteNonQuery()
                    Catch ex As OleDbException
                        _OutputDataERR(ex.Message)
                        Exit Function
                    End Try
                    ReDim byteSendData(16)
                    dimcount = 16
                    '*******メッセージヘッダー作成********
                    byteSendData(0) = CType("&H04", Byte)
                    byteSendData(1) = CType("&H01", Byte)
                    byteSendData(2) = 0 '2,3バイト目でレコード長
                    byteSendData(3) = CType("&H11", Byte) '2,3バイト目でレコード長
                    byteSendData(4) = 0
                    byteSendData(5) = CType("&H33", Byte)
                    byteSendData(6) = CType("&H01", Byte)
                    byteSendData(7) = 0
                    byteSendData(8) = CType("&HFD", Byte)
                    byteSendData(9) = CType("&H10", Byte)
                    byteSendData(10) = 0
                    If InStr(UCase(Trim(SQLstring)), "INSERT") Then
                        byteSendData(11) = CType("&HC3", Byte)
                    Else
                        byteSendData(11) = CType("&HC5", Byte)
                    End If
                    byteSendData(12) = 0
                    byteSendData(13) = CType("&H01", Byte)
                    byteSendData(14) = 0
                    byteSendData(15) = 0
                    byteSendData(16) = 0

                    state.send_buffer = byteSendData
                    SendToClient(state) '*******クライアントへ送信********
                End If
            Case Else
                AppSQLPrxyReceiveFromClient = Nothing
        End Select
    End Function
    Private Sub AppCallbackReceive(ByRef state As StateObject, ByVal bytesRead As Integer)
        'ASTサーバーがクライアントプログラムからデータを受け取ったときに実行
        state.binWriter.BaseStream.Position = 0
        state.binWriter.Write(state.buffer, 0, bytesRead)
        Dim binReader As New BinaryReader(state.binWriter.BaseStream)
        binReader.BaseStream.Position = 0
        state.stateclient.send_buffer = binReader.ReadBytes(bytesRead)

        OutputByteData(state.stateclient.send_buffer, 1)

        statebuff = state
        If start_flg Then
            ButtonEnabledToServer(True)
            ButtonEnabledToClient(False)
        Else
            RequestToSQLserver(state)
        End If

        state.workSocket.BeginReceive(state.buffer, 0, 1024, 0, New AsyncCallback(AddressOf CallbackReceive), state)
    End Sub
    Private Sub RequestToSQLserver(ByRef state As StateObject)
        'SQL Serverに送信するためのプロシージャを呼び出す
        Dim byteData As Byte() = state.stateclient.send_buffer
        Dim sParam As String = ""
        If CStr(Hex(byteData(0))) = "1" Then 'select insert update
            Dim objEnc As Encoding = Encoding.GetEncoding("utf-16")
            sParam = objEnc.GetString(byteData)
            sParam = Mid(sParam, 5, sParam.Length - 4)
        End If
        If Not customize_flg Then '「カスタマイズ」チェックボックス
            'カスタマイズ用イベントプロシージャの呼び出し
            byteData = AppSQLPrxyReceiveFromClient(sParam, state)
        Else
            byteData = state.stateclient.send_buffer
        End If
        If Not byteData Is Nothing Then
            'SQL Serverに送信するためのプロシージャ
            WithSvrRequestToSQLserver(state, Trim(txtIP.Text), Val(txtSQLPort.Text))
        End If
    End Sub
    Private Sub WithSvrRequestToSQLserver(ByRef state As StateObject, ByVal hostname As String, ByVal port As Integer)
        '1回目はサーバーに接続してから送信
        If state.stateclient.connection_flg = False Then
            state.stateclient.connection_flg = True
            WithSvrServerAddress = hostname
            WithSvrPortNumber = port
            'サーバーに接続して送信するためのプロシージャ
            WithSvrConnectToServer(state)
        Else '2回目以降送信のみ
            WithSvr_Send(state.stateclient.workSocket, state.stateclient.send_buffer)
            OutputByteData(state.stateclient.send_buffer, 4)
        End If
    End Sub
    Private Sub WithSvr_Send(ByVal SocketHandler As Socket, ByVal byteData As Byte())
        SocketHandler.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf WithSvrCallbackSend), SocketHandler)
    End Sub
    Private Sub WithSvrAppCallbackReceive(ByRef state As StateObject, ByVal bytesRead As Integer)
        Dim SocketHandler As Socket = state.stateclient.workSocket
        If bytesRead > 0 Then
            state.stateclient.binWriter.BaseStream.Position = 0
            state.stateclient.binWriter.Write(state.stateclient.buffer, 0, bytesRead)
            Dim binReader As New BinaryReader(state.stateclient.binWriter.BaseStream)
            binReader.BaseStream.Position = 0
            state.send_buffer = binReader.ReadBytes(bytesRead)
            '画面に表示
            OutputByteData(state.send_buffer, 2)
            'クライアントに送信するプロシージャ
            SendToClient(state)
            SocketHandler.BeginReceive(state.stateclient.buffer, 0, _buffer_size, 0, New AsyncCallback(AddressOf WithSvrCallbackReceive), state)
        End If
    End Sub
    Private Sub SendToClient(ByRef state As StateObject)
        'クライアント部がSQL Serverから受信した時
        statebuff = state
        If start_flg Then '「自動的に送信しない」チェックボックス
            '手動送信ボタンの状態変更
            ButtonEnabledToServer(False)
            ButtonEnabledToClient(True)
        Else
            'クライアントプログラムに送信
            _SendOne(state.workSocket, state.send_buffer)
            '画面に表示
            OutputByteData(state.send_buffer, 3)
        End If
    End Sub
    Private Sub OutputByteData(ByVal byteData As Byte(), ByVal select_param As Integer)
        '送受信したデータを画面に表示
        Dim i As Integer
        Dim sParam As String
        Dim strA As String
        Dim sOutParam As String = ""
        Dim sOutParam2 As String = ""

        Select Case select_param
            Case 1
                _OutputData(vbCrLf & vbCrLf & "*以下の" & CStr(byteData.Length) & " byteを受信*" & vbCrLf)
                _OutputData2(vbCrLf & vbCrLf & "*" & CStr(byteData.Length) & " byte*" & vbCrLf)
            Case 2
                _OutputDataS(vbCrLf & vbCrLf & "*以下の" & CStr(byteData.Length) & " byteを受信*" & vbCrLf)
                _OutputDataS2(vbCrLf & vbCrLf & "*" & CStr(byteData.Length) & " byte*" & vbCrLf)
            Case 3
                _OutputData3(vbCrLf & vbCrLf & "*以下の" & CStr(byteData.Length) & " byteを送信*" & vbCrLf)
                _OutputData4(vbCrLf & vbCrLf & "*" & CStr(byteData.Length) & " byte*" & vbCrLf)
            Case 4
                _OutputDataS3(vbCrLf & vbCrLf & "*以下の" & CStr(byteData.Length) & " byteを送信*" & vbCrLf)
                _OutputDataS4(vbCrLf & vbCrLf & "*" & CStr(byteData.Length) & " byte*" & vbCrLf)
        End Select

        For i = 0 To byteData.Length - 1
            sParam = CStr(Hex(byteData(i)))
            If sParam.Length = 1 Then
                sParam = "0" & sParam
            End If
            If byteData(i) = 0 Or byteData(i) > 127 Then
                strA = " "
            Else
                strA = Chr(byteData(i))
            End If
            If ((i + 1) Mod 8) = 0 Then
                sOutParam = sOutParam & sParam & vbCrLf
                sOutParam2 = sOutParam2 & strA & vbCrLf
            Else
                sOutParam = sOutParam & sParam & " "
                sOutParam2 = sOutParam2 & strA
            End If
        Next
        If sOutParam <> "" Then
            Select Case select_param
                Case 1
                    _OutputData(sOutParam)
                    _OutputData2(sOutParam2)
                Case 2
                    _OutputDataS(sOutParam)
                    _OutputDataS2(sOutParam2)
                Case 3
                    _OutputData3(sOutParam)
                    _OutputData4(sOutParam2)
                Case 4
                    _OutputDataS3(sOutParam)
                    _OutputDataS4(sOutParam2)
            End Select
        End If

    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
        Dim ipAddress As IPAddress
        Dim i As Integer
        For i = 0 To UBound(ipHostInfo.AddressList)
            ipAddress = ipHostInfo.AddressList(i)
            ComboIP.Items.Add(ipAddress.ToString)
        Next
        ComboIP.SelectedIndex = 0
        btnSendToSQL.Enabled = False
        btnSendToClient.Enabled = False
    End Sub
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        _PortNumber = Val(txtPort.Text)
        _NICNo = ComboIP.SelectedIndex
        start_flg = chkStart.Checked
        customize_flg = chkProc.Checked
        '------------
        If Not customize_flg Then
            SQLPrxyStartObj = AppSQLPrxyStart()
        End If
        '------------

        'TCP/IPサービスを開始
        _StartService()
        btnStart.Enabled = False
        ComboIP.Enabled = False
        chkProc.Enabled = False
        '手動送信ボタンの初期化
        If start_flg Then
            btnSendToSQL.Enabled = False
            btnSendToClient.Enabled = False
        End If
    End Sub
    Private Sub btnSendToClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendToClient.Click
        _SendOne(statebuff.workSocket, statebuff.send_buffer)
        OutputByteData(statebuff.send_buffer, 3)
        btnSendToClient.Enabled = False
    End Sub
    Private Sub btnSendToSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendToSQL.Click
        RequestToSQLserver(statebuff)
        btnSendToSQL.Enabled = False
    End Sub
    Private Sub chkStart_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkStart.CheckedChanged
        start_flg = chkStart.Checked
        btnSendToSQL.Enabled = chkStart.Checked
        btnSendToClient.Enabled = chkStart.Checked
    End Sub
    Private Sub chkProc_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkProc.CheckedChanged
        customize_flg = chkProc.Checked
    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtList.Text = ""
        txtList2.Text = ""
    End Sub
    Private Sub btnSQLClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSQLClear.Click
        txtSQLList.Text = ""
        txtSQLList2.Text = ""
    End Sub
    Private Sub btnClear2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear2.Click
        txtListToClient.Text = ""
        txtListToClient2.Text = ""
    End Sub
    Private Sub btnClear3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear3.Click
        txtSQLListToServer.Text = ""
        txtSQLListToServer2.Text = ""
    End Sub
    Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
        txtList.Text = ""
        txtList2.Text = ""
        txtSQLList.Text = ""
        txtSQLList2.Text = ""
        txtListToClient.Text = ""
        txtListToClient2.Text = ""
        txtSQLListToServer.Text = ""
        txtSQLListToServer2.Text = ""
    End Sub

    Private Function GetPathName() As String
        Dim fPathName As String
        Dim LenPath As Integer
        fPathName = System.Reflection.Assembly.GetExecutingAssembly.Location()
        LenPath = Len(fPathName) - 16 '16:SQLPXYServer.exeの文字数
        fPathName = Microsoft.VisualBasic.Left(fPathName, LenPath)
        GetPathName = fPathName
    End Function

    Private Sub WithSvrAppCallbackConnectClient(ByVal SocketHandler As Socket, ByRef state As StateObject)
        WithSvr_Send(SocketHandler, state.stateclient.send_buffer)
        OutputByteData(state.stateclient.send_buffer, 4)
    End Sub
    Private Sub WithSvr_Close(ByRef state As StateObject)
        state.stateclient.workSocket.Shutdown(SocketShutdown.Both)
        state.stateclient.workSocket.Close()
    End Sub

    Private Sub WithSvrConnectToServer(ByRef state As StateObject)
        Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(WithSvrServerAddress)
        Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
        Dim remoteEP As New IPEndPoint(ipAddress, WithSvrPortNumber)
        Dim SocketClient As Socket
        SocketClient = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        state.stateclient.workSocket = SocketClient
        SocketClient.BeginConnect(remoteEP, New AsyncCallback(AddressOf WithSvrCallbackConnectToServer), state)
    End Sub

    Private Sub WithSvrCallbackConnectToServer(ByVal ar As IAsyncResult)
        Dim state As StateObject = CType(ar.AsyncState, StateObject)
        Dim SocketHandler As Socket = state.stateclient.workSocket
        Try
            SocketHandler.EndConnect(ar)
        Catch e As SocketException
            _OutputDataS("サーバーに接続できませんでした:" & e.Message)
            'SocketHandler.Shutdown(SocketShutdown.Both)
            SocketHandler.Close()
            Exit Sub
        End Try
        'ユーザープログラミング用イベントプロシージャの呼び出し
        WithSvrAppCallbackConnectClient(SocketHandler, state)

        ReDim state.stateclient.buffer(_buffer_size)

        SocketHandler.BeginReceive(state.stateclient.buffer, 0, _buffer_size, 0, New AsyncCallback(AddressOf WithSvrCallbackReceive), state)
    End Sub
    Private Sub WithSvrCallbackReceive(ByVal ar As IAsyncResult)
        Dim state As StateObject = CType(ar.AsyncState, StateObject)
        Dim SocketHandler As Socket = state.stateclient.workSocket
        Dim bytesRead As Integer
        Try
            bytesRead = SocketHandler.EndReceive(ar)
        Catch
        End Try
        'ユーザープログラミング用イベントプロシージャの呼び出し
        WithSvrAppCallbackReceive(state, bytesRead)
    End Sub
    Private Sub WithSvrCallbackSend(ByVal ar As IAsyncResult)
        Dim bytesSent As Integer = ar.AsyncState.EndSend(ar)
    End Sub
    Private Sub ButtonEnabledToServer(ByVal bflg As Boolean)
        button_flg = bflg
        Me.Invoke(New AsyncServerDelegate(AddressOf EnabledToServer))
    End Sub
    Private Sub ButtonEnabledToClient(ByVal bflg As Boolean)
        button_flg = bflg
        Me.Invoke(New AsyncServerDelegate(AddressOf EnabledToClient))
    End Sub
    Private Sub EnabledToServer()
        Me.btnSendToSQL.Enabled = button_flg
    End Sub
    Private Sub EnabledToClient()
        Me.btnSendToClient.Enabled = button_flg
    End Sub
    Private Sub AppCallbackAccept(ByVal SocketHandler As Socket)
    End Sub
    Private Sub AppCallbackSend(ByVal SocketHandler As Socket)

    End Sub
    Private Sub AppDisconnectionUser(ByVal SocketHandler As Socket)

    End Sub
    Private Sub AppOutputData()
        Me.txtList.Text = Me.txtList.Text & DispDataBuf
    End Sub
    Private Sub AppOutputData2()
        Me.txtList2.Text = Me.txtList2.Text & DispDataBuf2
    End Sub
    Private Sub AppOutputData3()
        Me.txtListToClient.Text = Me.txtListToClient.Text & DispDataBuf3
    End Sub
    Private Sub AppOutputData4()
        Me.txtListToClient2.Text = Me.txtListToClient2.Text & DispDataBuf4
    End Sub
    Private Sub AppOutputDataS()
        Me.txtSQLList.Text = Me.txtSQLList.Text & DispDataBufS
    End Sub
    Private Sub AppOutputDataS2()
        Me.txtSQLList2.Text = Me.txtSQLList2.Text & DispDataBufS2
    End Sub
    Private Sub AppOutputDataS3()
        Me.txtSQLListToServer.Text = Me.txtSQLListToServer.Text & DispDataBufS3
    End Sub
    Private Sub AppOutputDataS4()
        Me.txtSQLListToServer2.Text = Me.txtSQLListToServer2.Text & DispDataBufS4
    End Sub
    Private Sub AppOutputDataSQL()
        Me.txtSQL.Text = DispDataBufSQL
    End Sub
    Private Sub AppOutputDataERR()
        Me.txtErr.Text = DispDataBufERR
    End Sub
    Private Sub _OutputData(ByVal strDisp As String)
        DispDataBuf = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputData))
    End Sub
    Private Sub _OutputData2(ByVal strDisp As String)
        DispDataBuf2 = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputData2))
    End Sub
    Private Sub _OutputData3(ByVal strDisp As String)
        DispDataBuf3 = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputData3))
    End Sub
    Private Sub _OutputData4(ByVal strDisp As String)
        DispDataBuf4 = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputData4))
    End Sub
    Private Sub _OutputDataS(ByVal strDisp As String)
        DispDataBufS = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputDataS))
    End Sub
    Private Sub _OutputDataS2(ByVal strDisp As String)
        DispDataBufS2 = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputDataS2))
    End Sub
    Private Sub _OutputDataS3(ByVal strDisp As String)
        DispDataBufS3 = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputDataS3))
    End Sub
    Private Sub _OutputDataS4(ByVal strDisp As String)
        DispDataBufS4 = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputDataS4))
    End Sub
    Private Sub _OutputDataSQL(ByVal strDisp As String)
        DispDataBufSQL = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputDataSQL))
    End Sub
    Private Sub _OutputDataERR(ByVal strDisp As String)
        DispDataBufERR = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputDataERR))
    End Sub

    Private Sub _StartService()
        Timer1.Interval() = _TimeOutCheckInterval
        If _TimeOutCheckType <> "NoCheck" Then
            Timer1.Enabled = True
        End If

        Accept()
    End Sub
    Private Sub CallbackReceive(ByVal ar As IAsyncResult)
        Dim state As StateObject = CType(ar.AsyncState, StateObject)
        Dim SocketHandler As Socket = state.workSocket
        Dim bytesRead As Integer
        Try
            bytesRead = SocketHandler.EndReceive(ar)
        Catch
        End Try

        If bytesRead > 0 Then
            AppCallbackReceive(state, bytesRead)

            If _TimeOutCheckType <> "NoCheck" Then
                TimeSet(SocketHandler)
            End If
        End If

    End Sub

    Private Sub _SendOne(ByVal SocketHandler As Socket, ByVal byteData As Byte())
        Dim i As Integer
        Try
            SocketHandler.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf CallbackSend), SocketHandler)
        Catch e As SocketException
            Debug.Write(vbCrLf & "***" & e.SocketErrorCode & ":" & e.Message)
            If e.SocketErrorCode <> 10054 Then '10054:既存の接続はリモート ホストに強制的に切断されました。
                For i = 0 To ClientAccesses.Count - 1
                    If CType(ClientAccesses(i), Socket) Is SocketHandler Then
                        RemoveUser(i)
                        Exit For
                    End If
                Next
            End If
        End Try
    End Sub

    Private Sub _SendAll(ByVal SocketHandler As Socket, ByVal byteData As Byte())
        Dim SocketH As Socket
        Dim i As Integer
        Dim ErrNo(0) As Integer
        For i = 0 To ClientAccesses.Count - 1
            Try
                SocketH = CType(ClientAccesses(i), Socket)
                SocketH.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf CallbackSend), SocketH)
            Catch e As Exception
                ReDim Preserve ErrNo(UBound(ErrNo) + 1)
                ErrNo(UBound(ErrNo)) = i
            End Try
        Next

        If UBound(ErrNo) > 0 Then
            For i = UBound(ErrNo) To 1 Step -1
                RemoveUser(ErrNo(i))
            Next
        End If
    End Sub

    Private Sub _DisconnectionUser(ByVal SocketHandler As Socket)
        Dim i As Integer
        For i = 0 To ClientAccesses.Count - 1
            If CType(ClientAccesses(i), Socket) Is SocketHandler Then
                RemoveUser(i)
                Exit For
            End If
        Next

        AppDisconnectionUser(SocketHandler)

        '切断処理
        SocketHandler.Shutdown(SocketShutdown.Both)
        SocketHandler.Close()
    End Sub
    Private Sub Accept()
        Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
        Dim ipAddress As IPAddress = ipHostInfo.AddressList(_NICNo)
        Dim localEndPoint As New IPEndPoint(ipAddress, _PortNumber)

        ClientAccesses = New ArrayList()
        SocketListener.Bind(localEndPoint)
        SocketListener.Listen(100)

        SocketListener.BeginAccept(New AsyncCallback(AddressOf CallbackAccept), SocketListener)
    End Sub

    Private Sub CallbackAccept(ByVal ar As IAsyncResult)
        Debug.Write(vbCrLf & "CallbackAccept")
        Dim SocketHandler As Socket = ar.AsyncState.EndAccept(ar)
        Dim state As New StateObject()

        ClientAccesses.Add(SocketHandler)
        ReDim Preserve Time_ClientAccesses(ClientAccesses.Count)
        Time_ClientAccesses(ClientAccesses.Count - 1) = DateTime.Now

        'ユーザープログラミング用イベントプロシージャの呼び出し
        AppCallbackAccept(SocketHandler)

        SocketListener.BeginAccept(New AsyncCallback(AddressOf CallbackAccept), SocketListener)

        ReDim state.buffer(_buffer_size)
        state.workSocket = SocketHandler

        Try
            SocketHandler.BeginReceive(state.buffer, 0, _buffer_size, 0, New AsyncCallback(AddressOf CallbackReceive), state)
        Catch ex As SocketException
            Debug.Write(ex.Message)
        End Try
    End Sub

    Private Sub RemoveUser(ByVal uIndex As Integer)
        Dim i As Integer
        For i = uIndex + 1 To ClientAccesses.Count - 1
            '配列要素を前に詰める
            Time_ClientAccesses(i) = Time_ClientAccesses(i + 1)
        Next
        'ユーザー数を1減らす
        ClientAccesses.RemoveAt(uIndex)
    End Sub
    Private Sub CallbackSend(ByVal ar As IAsyncResult)
        Dim bytesSent As Integer = ar.AsyncState.EndSend(ar)
        If _TimeOutCheckType <> "NoCheck" Then
            TimeSet(ar.AsyncState)
        End If
        AppCallbackSend(ar.AsyncState)
    End Sub
    Private Sub TimeSet(ByVal SocketHandler As Socket)
        Dim i As Integer
        For i = 0 To ClientAccesses.Count - 1
            If CType(ClientAccesses(i), Socket) Is SocketHandler Then
                Time_ClientAccesses(i + 1) = Now
                Exit For
            End If
        Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim i As Integer

        For i = ClientAccesses.Count - 1 To 0 Step -1
            '現在と比べて指定時間以上経過していたら
            If DateTime.Compare(DateTime.Now, Time_ClientAccesses(i).AddMilliseconds(_TimeOut)) > 0 Then
                Try
                    Dim SocClient As Socket = CType(ClientAccesses(i), Socket)
                    If _TimeOutCheckType = "Time" Then
                        RemoveUser(i)
                        '切断処理
                        SocClient.Shutdown(SocketShutdown.Both)
                        SocClient.Close()
                    Else 'DataSend
                        _SendOne(SocClient, _CheckTimeOutData)
                    End If
                Catch ex As Exception
                    _OutputData(vbCrLf & ex.ToString())
                End Try
            End If
        Next

    End Sub

End Class

Public Class StateObject
    Public workSocket As Socket = Nothing
    Public buffer() As Byte
    Public sb As New StringBuilder()
    Public send_buffer As Byte()
    Public binWriter As New BinaryWriter(New MemoryStream())
    Public stateclient As New StateObjectClient
End Class
Public Class StateObjectClient
    Public workSocket As Socket = Nothing
    Public buffer() As Byte
    Public sb As New StringBuilder()
    Public send_buffer As Byte()
    Public binWriter As New BinaryWriter(New MemoryStream())
    Public connection_flg As Boolean = False 'サーバーとの接続用
End Class

Public Class WaitFlgObject
    Public Flg As Integer = 0
End Class

