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

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
    Private IsSQLServer As Integer


#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
    Friend WithEvents Label14 As System.Windows.Forms.Label
    Friend WithEvents txtConnectionString As System.Windows.Forms.TextBox
    Friend WithEvents Label15 As System.Windows.Forms.Label
    Friend WithEvents Label17 As System.Windows.Forms.Label
    Friend WithEvents txtSQLDB As System.Windows.Forms.TextBox
    <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.Label14 = New System.Windows.Forms.Label
        Me.txtConnectionString = New System.Windows.Forms.TextBox
        Me.Label15 = New System.Windows.Forms.Label
        Me.Label17 = New System.Windows.Forms.Label
        Me.txtSQLDB = New System.Windows.Forms.TextBox
        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, 452)
        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, 452)
        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, 485)
        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, 488)
        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.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label19.Location = New System.Drawing.Point(123, 399)
        Me.Label19.Name = "Label19"
        Me.Label19.Size = New System.Drawing.Size(390, 12)
        Me.Label19.TabIndex = 66
        Me.Label19.Text = "-------------------------------------------------------"
        '
        '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)
        '
        'Label14
        '
        Me.Label14.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label14.Location = New System.Drawing.Point(15, 418)
        Me.Label14.Name = "Label14"
        Me.Label14.Size = New System.Drawing.Size(74, 13)
        Me.Label14.TabIndex = 7
        Me.Label14.Text = "接続文字列"
        '
        'txtConnectionString
        '
        Me.txtConnectionString.Location = New System.Drawing.Point(84, 414)
        Me.txtConnectionString.Name = "txtConnectionString"
        Me.txtConnectionString.Size = New System.Drawing.Size(472, 19)
        Me.txtConnectionString.TabIndex = 6
        Me.txtConnectionString.Text = "Provider=SQLOLEDB;Server=localhost;user id=sa;password=pass;Database=pubs;Connect" & _
            " Timeout=6000"
        '
        'Label15
        '
        Me.Label15.AutoSize = True
        Me.Label15.Location = New System.Drawing.Point(139, 437)
        Me.Label15.Name = "Label15"
        Me.Label15.Size = New System.Drawing.Size(318, 12)
        Me.Label15.TabIndex = 68
        Me.Label15.Text = "(接続するSQL Serverを「Start」ボタンをクリックする前に指定します)"
        '
        'Label17
        '
        Me.Label17.AutoSize = True
        Me.Label17.Font = New System.Drawing.Font("MS UI Gothic", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
        Me.Label17.Location = New System.Drawing.Point(16, 515)
        Me.Label17.Name = "Label17"
        Me.Label17.Size = New System.Drawing.Size(60, 24)
        Me.Label17.TabIndex = 70
        Me.Label17.Text = "テーブル名" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "列名と値"
        '
        'txtSQLDB
        '
        Me.txtSQLDB.Location = New System.Drawing.Point(84, 515)
        Me.txtSQLDB.Multiline = True
        Me.txtSQLDB.Name = "txtSQLDB"
        Me.txtSQLDB.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSQLDB.Size = New System.Drawing.Size(476, 30)
        Me.txtSQLDB.TabIndex = 69
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(588, 550)
        Me.Controls.Add(Me.Label17)
        Me.Controls.Add(Me.txtSQLDB)
        Me.Controls.Add(Me.Label20)
        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.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.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.txtConnectionString)
        Me.Controls.Add(Me.txtPort)
        Me.Controls.Add(Me.label1)
        Me.Controls.Add(Me.btnStart)
        Me.Controls.Add(Me.Label12)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label11)
        Me.Controls.Add(Me.Label19)
        Me.Controls.Add(Me.Label18)
        Me.Controls.Add(Me.Label14)
        Me.Controls.Add(Me.Label15)
        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
        'SQL Serverに接続するための接続文字列
        Dim ConnectionString As String = Trim(txtConnectionString.Text)
        'Jetデータベースエンジンの場合
        'Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Trim(txtDB.Text)
        'SQL Serverか否かのフラグを設定
        If InStr(UCase(ConnectionString), "PROVIDER=SQLOLEDB") > 0 Then
            'SQL Serverの場合
            IsSQLServer = 0
        ElseIf InStr(UCase(ConnectionString), "PROVIDER=MICROSOFT.JET") > 0 Then
            IsSQLServer = 1
        Else
            IsSQLServer = 2
        End If
        Dim DBCon As New OleDbConnection
        DBCon.ConnectionString = ConnectionString
        DBCon.Open()
        AppSQLPrxyStart = DBCon
        txtConnectionString.Enabled = False
    End Function
    Private Function AppSQLPrxyReceiveFromClient(ByRef state As StateObject) As Byte()
        'カスタマイズしない場合は、次の1行を残す
        'AppSQLPrxyReceiveFromClient = state.stateclient.send_buffer
        AppSQLPrxyReceiveFromClient = Nothing
        Dim byteData As Byte() = state.stateclient.send_buffer
        Dim SQLstring As String = ""
        Dim nRet As Integer
        Dim sTableName As String = ""
        Dim dParamList(1, 1) As String
        Dim sOutPut As String
        Dim loopcount As Integer
        Dim sSQL As String = ""

        Select Case CStr(Hex(byteData(0)))
            Case "12", "10" '接続要求
                'SQL Serverに転送
                AppSQLPrxyReceiveFromClient = state.stateclient.send_buffer
                'クライアントがSQLプロキシサーバーに接続する場合は上の行をリマークし、下のリマークをはずす
                If CStr(Hex(byteData(0))) = "12" Then
                    '1回目
                    'SQLPrxyResponseConnect(12, state)
                Else
                    '2回目
                    'SQLPrxyResponseConnect(10, state)
                End If
            Case "1", "3" '受信したメッセージがSQL文の場合
                '1:文字列によるSQL文の場合  3:DataSetオブジェクトによる更新の場合
                If CStr(Hex(byteData(0))) = "1" Then 'SELECT, INSERT, UPDATE
                    'byteDataを文字列に変換
                    Dim objEnc As Encoding = Encoding.GetEncoding("utf-16")
                    SQLstring = objEnc.GetString(byteData)
                    SQLstring = Mid(SQLstring, 5, SQLstring.Length - 4)
                End If
                If CStr(Hex(byteData(0))) = "3" Then
                    'DataSetオブジェクトによる更新の場合、生成されたSQL文を標準のSQLに変換
                    SQLstring = UtilSQLPrxyUPDATEwithDataset(state)
                End If

                '受信したSQL文を解析
                nRet = UtilSQLPrxySQLStringAnalysis(SQLstring, sTableName, dParamList)
                'nRetの値はSELECT文の場合は1、INSERT文の場合は2、UPDATE文の場合は3、DELETE文の場合は4

                'ここから、解析内容をフォームに表示
                sOutPut = "テーブル名:" & sTableName & vbCrLf
                For loopcount = 0 To UBound(dParamList, 2)
                    sOutPut = sOutPut & " " & dParamList(0, loopcount)
                    If Not (dParamList(1, loopcount) Is Nothing) Then
                        sOutPut = sOutPut & "=" & dParamList(1, loopcount)
                    End If
                Next
                _OutputDataSQL(SQLstring)
                _OutputDataSQLDB(sOutPut)
                'ここまで、解析内容をフォームに表示
                If nRet = 4 And InStr(sTableName, "table1") > 0 Then
                    'DELETEはキャンセルして、クライアントに応答する。
                    SQLPrxyResponseUpdate(SQLstring, state, CStr(Hex(byteData(0))))
                Else
                    '条件に一致しない場合はサーバーに要求を転送
                    AppSQLPrxyReceiveFromClient = state.stateclient.send_buffer
                End If
            Case Else
                '条件に一致しない場合はサーバーに要求を転送
                AppSQLPrxyReceiveFromClient = state.stateclient.send_buffer
                'AppSQLPrxyReceiveFromClient = Nothing
        End Select
    End Function

    Private Function UtilSQLPrxySQLStringAnalysis(ByVal SQLString As String, ByRef sTable As String, ByRef dParam(,) As String) As Integer
        Dim sParam As String
        Dim nFROM As Integer
        Dim nPos As Integer
        Dim nP As Integer
        Dim nStart As Integer
        Dim nCnt As Integer
        Dim nEQ As Integer
        Dim sParamUpdate As String

        Select Case True
            Case InStr(UCase(SQLString), "SELECT") > 0
                nPos = InStr(UCase(SQLString), "SELECT")
                SQLString = Mid(SQLString, nPos)
                nFROM = InStr(UCase(SQLString), "FROM")
                nP = InStr(nFROM + 5, SQLString, " ")
                If nP <= 0 Then
                    nP = SQLString.Length
                End If
                sTable = Trim(Mid(SQLString, nFROM + 5, nP - nFROM - 4))
                sParam = Trim(Mid(SQLString, 8, nFROM - 8))

                ParamToDim(sParam, dParam, 0)

                UtilSQLPrxySQLStringAnalysis = 1

            Case InStr(UCase(SQLString), "INSERT") > 0
                nPos = InStr(UCase(SQLString), "INSERT")
                SQLString = Mid(SQLString, nPos)
                nFROM = InStr(UCase(SQLString), "INTO")
                nP = InStr(nFROM + 5, SQLString, " ")
                sTable = Trim(Mid(SQLString, nFROM + 5, nP - nFROM - 4))

                nPos = InStr(nP + 1, SQLString, "(")
                nP = InStr(nPos + 1, SQLString, ")")
                nFROM = InStr(nP, UCase(SQLString), "VALUES")
                sParam = Trim(Mid(SQLString, nPos + 1, nP - nPos - 1))
                ParamToDim(sParam, dParam, 0)

                nPos = InStr(nFROM + 6, SQLString, "(")
                nP = InStr(nPos + 1, SQLString, ")")
                sParam = Trim(Mid(SQLString, nPos + 1, nP - nPos - 1))
                nP = InStr(sParam, ",")
                nStart = 1
                nCnt = 0
                Do While nP > 0
                    dParam(1, nCnt) = Trim(Mid(sParam, nStart, nP - nStart))
                    nStart = nP + 1
                    nP = InStr(nP + 1, sParam, ",")
                    nCnt = nCnt + 1
                Loop
                dParam(1, nCnt) = Trim(Mid(sParam, nStart, sParam.Length - nStart + 1))

                UtilSQLPrxySQLStringAnalysis = 2

            Case InStr(UCase(SQLString), "UPDATE") > 0
                nPos = InStr(UCase(SQLString), "UPDATE")
                SQLString = Mid(SQLString, nPos)
                nP = InStr(8, SQLString, " ")
                sTable = Trim(Mid(SQLString, 8, nP - 7))
                nPos = InStr(UCase(SQLString), "SET")
                nP = InStr(nPos + 4, UCase(SQLString), "WHERE") - 1
                If nP <= 0 Then
                    nP = SQLString.Length
                End If
                sParam = Trim(Mid(SQLString, nPos + 4, nP - nPos - 3))

                nP = InStr(sParam, ",")
                nStart = 1
                nCnt = 0
                Do While nP > 0
                    ReDim Preserve dParam(1, nCnt)
                    sParamUpdate = Trim(Mid(sParam, nStart, nP - nStart))
                    nEQ = InStr(sParamUpdate, "=")
                    dParam(0, nCnt) = Trim(Mid(sParamUpdate, 1, nEQ - 1))
                    dParam(1, nCnt) = Trim(Mid(sParamUpdate, nEQ + 1, sParamUpdate.Length - nEQ))
                    nStart = nP + 1
                    nP = InStr(nP + 1, sParam, ",")
                    nCnt = nCnt + 1
                Loop
                ReDim Preserve dParam(1, nCnt)
                sParamUpdate = Trim(Mid(sParam, nStart, sParam.Length - nStart + 1))
                nEQ = InStr(sParamUpdate, "=")
                dParam(0, nCnt) = Trim(Mid(sParamUpdate, 1, nEQ - 1))
                dParam(1, nCnt) = Trim(Mid(sParamUpdate, nEQ + 1, sParamUpdate.Length - nEQ))

                UtilSQLPrxySQLStringAnalysis = 3

            Case InStr(UCase(SQLString), "DELETE") > 0
                nPos = InStr(UCase(SQLString), "DELETE")
                SQLString = Mid(SQLString, nPos)
                nFROM = InStr(UCase(SQLString), "FROM")
                nP = InStr(nFROM + 5, SQLString, " ")
                If nP <= 0 Then
                    nP = SQLString.Length
                End If
                sTable = Trim(Mid(SQLString, nFROM + 5, nP - nFROM - 4))
                UtilSQLPrxySQLStringAnalysis = 4
        End Select
    End Function
    Private Sub ParamToDim(ByVal sParam As String, ByRef dParam(,) As String, ByVal nDim As Integer)
        Dim nP As Integer
        Dim nStart As Integer
        Dim nCnt As Integer

        nP = InStr(sParam, ",")
        nStart = 1
        nCnt = 0
        Do While nP > 0
            ReDim Preserve dParam(1, nCnt)
            dParam(nDim, nCnt) = Trim(Mid(sParam, nStart, nP - nStart))
            nStart = nP + 1
            nP = InStr(nP + 1, sParam, ",")
            nCnt = nCnt + 1
        Loop
        ReDim Preserve dParam(1, nCnt)
        dParam(nDim, nCnt) = Trim(Mid(sParam, nStart, sParam.Length - nStart + 1))

    End Sub
    Private Sub SQLPrxyResponseConnect(ByVal con_flg As Integer, ByRef state As StateObject)
        Dim byteSendData As Byte()
        Dim cOffset As Integer
        Select Case con_flg
            Case 12 '接続要求の1回目
                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 00 " _
                                     & "C2 00 00 02 00 "
                ReDim byteSendData(data12.Length / 3 - 1)
                cOffset = JetDBSvrString16toBinary(byteSendData, data12, 0)

                state.send_buffer = byteSendData
                'クライアントへ送信
                SendToClient(state)

            Case 10 '接続要求の2回目
                Dim data10 As String = "04 01 01 2D 00 33 01 00-E3 1B 00 01 06 73 00 61 " _
                                     & "00 6D 00 70 00 6C 00 65-00 06 6D 00 61 00 73 00 " _
                                     & "74 00 65 00 72 00 AB 58-00 45 16 00 00 02 00 21 " _
                                     & "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 73 00 61 00 6D 00 70-00 6C 00 65 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 07 01 00 00 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 00 C2 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)
                cOffset = JetDBSvrString16toBinary(byteSendData, data10, 0)

                state.send_buffer = byteSendData
                'クライアントへ送信
                SendToClient(state)

        End Select
    End Sub
    Private Sub SQLPrxyResponseSelect(ByVal SQLstring As String, ByRef state As StateObject)
        'SELECT文の場合
        Dim command As OleDbCommand = New OleDbCommand(Trim(SQLstring), SQLPrxyStartObj)
        Dim byteSendData As Byte()
        Dim cOffset As Integer
        Dim SAdapter As OleDbDataAdapter = New OleDbDataAdapter()
        '主キー情報も取得する
        SAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
        SAdapter.SelectCommand = command
        Dim jetDBtable As New DataTable()

        Try
            SAdapter.Fill(jetDBtable)
        Catch ex As OleDbException
            _OutputDataERR(ex.Message)
            Exit Sub
        End Try

        Dim jetDBcolArr() As DataColumn
        jetDBcolArr = jetDBtable.PrimaryKey
        'Debug.Write("Column Count: " & jetDBcolArr.Length.ToString())

        ReDim byteSendData(_buffer_size)

        'メッセージヘッダー&列名ヘッダー&列名データ作成
        Dim strHeader As String = "04 01 00 00 00 33 01 00 81 "
        cOffset = JetDBSvrCreateMsgHeader(byteSendData, jetDBtable, jetDBcolArr, strHeader)

        CheckByteArraySize(byteSendData, cOffset, 1)
        '取得した結果セットをメッセージに埋め込む
        cOffset = JetDBSvrCreateData(byteSendData, jetDBtable, jetDBcolArr, cOffset)

        CheckByteArraySize(byteSendData, cOffset, 9)
        'フッター作成
        Dim datafoot As String = "FD 10 00 C1 00 01 00 00 00 "
        cOffset = JetDBSvrString16toBinary(byteSendData, datafoot, cOffset)

        '全メッセージ長を求め、メッセージの3,4バイトに格納
        If cOffset < 255 Then
            byteSendData(3) = CType(cOffset + 1, Byte) '全レコード長
        Else
            Dim int1, int2 As Integer
            int1 = (cOffset + 1) \ 256
            int2 = (cOffset + 1) Mod 256
            byteSendData(2) = CType(int1, Byte) '全レコード長
            byteSendData(3) = CType(int2, Byte) '全レコード長
        End If

        ReDim Preserve byteSendData(cOffset)
        'クライアントへ送信
        state.send_buffer = byteSendData
        SendToClient(state)

    End Sub
    Private Function SQLPrxyNonQueryToServer(ByVal SQLstring As String) As Boolean

        SQLPrxyNonQueryToServer = True
        Dim command As OleDbCommand = New OleDbCommand(Trim(SQLstring), SQLPrxyStartObj)
        _OutputDataSQL(SQLstring)
        Try
            command.ExecuteNonQuery()
        Catch ex As OleDbException
            _OutputDataERR(ex.Message)
            SQLPrxyNonQueryToServer = False
        End Try

    End Function
    Private Sub SQLPrxyResponseUpdate(ByVal SQLstring As String, ByRef state As StateObject, ByVal update_flg As String)
        'INSERT文、UPDATE文の場合
        Dim byteSendData As Byte()
        Dim cOffset As Integer
        Dim strInsertUpdate As String

        If update_flg = "3" Then
            '3:DataSetオブジェクトによる更新の場合
            ReDim byteSendData(30)
            strInsertUpdate = "04 01 00 1F 00 33 01 00-FF 11 00 C5 00 01 00 00 " & _
                              "00 79 00 00 00 00 FE 00-00 E0 00 01 00 00 00 "
            cOffset = JetDBSvrString16toBinary(byteSendData, strInsertUpdate, 0)
        Else
            '1:文字列によるSQL文の場合
            ReDim byteSendData(16)
            'メッセージヘッダー作成
            strInsertUpdate = "04 01 00 11 00 33 01 00-FD 10 00 "
            '3,4バイト目はメッセージ長。固定である。
            cOffset = JetDBSvrString16toBinary(byteSendData, strInsertUpdate, 0)

            If InStr(UCase(Trim(SQLstring)), "INSERT") Then
                byteSendData(cOffset + 1) = CType("&HC3", Byte)
            ElseIf InStr(UCase(Trim(SQLstring)), "UPDATE") Then
                byteSendData(cOffset + 1) = CType("&HC5", Byte)
            Else
                byteSendData(cOffset + 1) = CType("&HC4", Byte)
            End If
            cOffset = cOffset + 2
            strInsertUpdate = "00 01 00 00 00 "
            cOffset = JetDBSvrString16toBinary(byteSendData, strInsertUpdate, cOffset)
        End If

        'クライアントへ送信
        state.send_buffer = byteSendData
        SendToClient(state)

    End Sub
    Private Sub UtilSQLPrxyReturnTableSchema(ByVal SQLstring As String, ByRef state As StateObject)
        Dim byteData As Byte() = state.stateclient.send_buffer
        Dim byteSendData As Byte()
        Dim cOffset As Integer
        Dim sqlparam As String
        Dim bytesEncodedCount As Integer
        Dim column As DataColumn
        Dim i As Integer
        Dim colcount As Integer

        Dim command As OleDbCommand = New OleDbCommand(Trim(SQLstring), SQLPrxyStartObj)
        Dim SAdapter As OleDbDataAdapter = New OleDbDataAdapter()
        'SELECT文の実行前に、データベースエンジンから、主キー情報も取得するようにプロパティ設定。返信相手のADO.NETクライアントに正しくUPDATE文を生成させるために必要
        SAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
        Dim jetDBtable As New DataTable()

        If InStr(LCase(SQLPrxyStartObj.ConnectionString), "provider=sqloledb") <= 0 Then
            'SQL Server以外の場合(Jetデータベースエンジンを含む)
            'Tarnsact-SQLのSETステートメントが使えないため、削除している。
            sqlparam = Trim(Mid(SQLstring, InStr(SQLstring, "SELECT"), InStr(InStr(SQLstring, "SELECT"), SQLstring, "SET") - InStr(SQLstring, "SELECT")))
            command.CommandText = sqlparam
        End If
        SAdapter.SelectCommand = command
        Try
            'テーブル構造を取得するためのSELECT文を実行するためだが、ADO.NETクライアントがキー情報を要求した場合は、データがあるSELECT文もここで実行される。つまり、前後にSETステートメントがついているSELECT文の実行である。
            SAdapter.Fill(jetDBtable)
        Catch ex As OleDbException
            _OutputDataERR(ex.Message)
            Exit Sub
        End Try

        Dim jetDBcolArr() As DataColumn
        'キー列情報を取得
        jetDBcolArr = jetDBtable.PrimaryKey
        'ヘッダーの生成
        ReDim byteSendData(_buffer_size)
        Dim sParam As String = "04 01 00 00 00 33 01 00-FD 01 00 BA 00 00 00 00 " & _
                               "00 FD 01 00 B9 00 00 00-00 00 "
        If InStr(SQLstring, "SET FMTONLY ON") > 0 Then
            'テーブル構造だけでデータを返さない場合(UPDATEの直前)
            sParam = sParam & "FD 01 00 B9 00 00 00 00 00 "
        End If
        sParam = sParam & "81 "
        'テーブル構造情報を生成してメッセージに埋め込む
        cOffset = JetDBSvrCreateMsgHeader(byteSendData, jetDBtable, jetDBcolArr, sParam)
        byteSendData(cOffset) = CType("&HA4", Byte)
        'テーブル名のデータサイズ(+2)を、後で代入するための要素
        byteSendData(cOffset + 1) = CType(0, Byte)
        byteSendData(cOffset + 2) = CType(0, Byte)
        'テーブル名の文字数
        byteSendData(cOffset + 3) = CType(jetDBtable.TableName.Length, Byte)
        '多分、以下のバイトもテーブル名のサイズ用だが、SQLプロキシサーバーは255文字までのテーブルにしか対応していない
        byteSendData(cOffset + 4) = 0
        'テーブル名をUNICODEに変換してメッセージに格納
        bytesEncodedCount = Encoding.Unicode.GetBytes(jetDBtable.TableName, 0, jetDBtable.TableName.Length, byteSendData, cOffset + 5)
        'テーブル名のデータサイズ(+2)を代入
        byteSendData(cOffset + 1) = CType(2 + bytesEncodedCount, Byte)
        cOffset = cOffset + 5 + bytesEncodedCount
        byteSendData(cOffset) = CType("&HA5", Byte)
        '以下、列数情報とキー情報
        'テーブルの列数に3をかけたもの
        byteSendData(cOffset + 1) = CType(jetDBtable.Columns.Count * 3, Byte)
        'SQLプロキシサーバーは255/3列までにしか対応していない
        byteSendData(cOffset + 2) = 0
        cOffset = cOffset + 3
        colcount = 0
        '列の数だけループする。
        For Each column In jetDBtable.Columns
            colcount = colcount + 1
            '1列目なら1、2列目なら2
            byteSendData(cOffset) = CType(colcount, Byte)
            byteSendData(cOffset + 1) = CType(1, Byte)
            'キー列でないなら0、キー列ならば以下のIf条件で8をいれる
            byteSendData(cOffset + 2) = CType(0, Byte)
            '多重キーの場合、多重化している数だけループ
            '多重キーでない場合は、1回のみ
            For i = 0 To jetDBcolArr.GetUpperBound(0)
                If column.ColumnName = jetDBcolArr(i).ColumnName Then
                    'もしキー列ならば、8を入れる
                    byteSendData(cOffset + 2) = CType(8, Byte)
                    Exit For
                End If
            Next i
            cOffset = cOffset + 3
        Next
        'フッター
        If InStr(SQLstring, "SET FMTONLY ON") > 0 Then
            'テーブル構造だけでデータを返さない場合(UPDATEの直前)
            sParam = "FD 01 00 C1 00 00 " & _
                     "00 00 00 FD 01 00 BA 00 00 00 00 00 " & _
                     "FD 00 00 BA 00 00 00 00 00 "
            cOffset = JetDBSvrString16toBinary(byteSendData, sParam, cOffset)
        Else
            'テーブル構造だけでなく、データを返す場合(キー列情報つきでSELECT文を実行した場合)
            '「JetDBSvrCreateData」プロシージャを呼び出してフッターの前にテーブルのデータを挿入している。
            cOffset = JetDBSvrCreateData(byteSendData, jetDBtable, jetDBcolArr, cOffset)
            sParam = "FD 01 00 C1 00 02 00 00 00 " & _
                     "FD 00 00 BA 00 00 00 00 00 "
            cOffset = JetDBSvrString16toBinary(byteSendData, sParam, cOffset)
        End If

        '全メッセージ長を求め、メッセージの3,4バイトに格納
        If cOffset < 255 Then
            'メッセージのサイズが255以下の場合
            byteSendData(3) = CType(cOffset + 1, Byte) '全レコード長
        Else
            Dim int1, int2 As Integer
            int1 = (cOffset + 1) \ 256
            int2 = (cOffset + 1) Mod 256
            byteSendData(2) = CType(int1, Byte) '全レコード長
            byteSendData(3) = CType(int2, Byte) '全レコード長
        End If

        ReDim Preserve byteSendData(cOffset)
        'クライアントへ送信
        state.send_buffer = byteSendData
        SendToClient(state)
    End Sub
    Private Function UtilSQLPrxyUPDATEwithDataset(ByRef state As StateObject) As String
        Dim byteData As Byte() = state.stateclient.send_buffer
        Dim SQLstring As String
        Dim param1 As Integer
        Dim param2 As Integer
        Dim param3 As Integer
        Dim i As Integer
        Dim Pstring As String
        Dim strP As String
        Dim strType As String
        Dim intP As Integer
        Dim nLen As Integer
        Dim sP As String
        Dim nP As Integer
        Dim n1 As Integer
        Dim n2 As Integer
        Dim datecount As Integer
        Dim dateTime1 As Date = #1/1/1900#
        Dim paramDate As Date

        param1 = 0
        param2 = 0
        'E7を2探す。その10バイト後からUPDATE文が開始される。
        For i = 0 To byteData.Length - 1
            If Hex(byteData(i)) = "E7" Then
                If Hex(byteData(i + 3)) = "11" And Hex(byteData(i + 4)) = "4" And Hex(byteData(i + 5)) = "D0" Then
                    If param1 = 0 Then
                        '1つ目のE7を探す
                        param1 = i + 10
                    Else '2つ目のE7を探す
                        param2 = i
                        Exit For
                    End If
                End If
            End If
        Next
        'UPDATE文を取り出してUNOCODEで変換
        SQLstring = Encoding.Unicode.GetString(byteData, param1, param2 - param1 - 1)
        '3を探す。3はパラメータのための区切り文字である。
        'アルファベットのUNICODEではつかわれないので誤動作はしない。
        For i = param2 + 10 To byteData.Length - 1
            If CStr(Hex(byteData(i))) = "3" Then
                param3 = i
                Exit For
            End If
        Next
        'パラメータの型リスト部を切り出し、UNOCODEで変換。
        Pstring = Encoding.Unicode.GetString(byteData, param2 + 10, byteData.Length - (param2 + 10) - (byteData.Length - param3))

        param1 = InStr(1, SQLstring, "@")
        Do While param1 > 0 'UPDATE文に@マークがある回数分(パラメータの数だけ)回る
            'UPDATE文の部分から、パラメータ名(@p1)を取り出す
            n1 = InStr(param1, SQLstring, ")")
            n2 = InStr(param1, SQLstring, " ")
            If (n1 < n2 Or n2 = 0) And n1 > 0 Then
                strP = Mid(SQLstring, param1, n1 - param1)
            Else
                strP = Mid(SQLstring, param1, n2 - param1)
                If Mid(strP, strP.Length) = "," Then
                    strP = Mid(strP, 1, strP.Length - 1)
                End If
            End If
            'パラメータの型リストから、パラメータ名(@p1など)を探し出し、データ型を取得(strType)
            intP = InStr(1, Pstring, strP)
            If InStr(intP, Pstring, ",") > 0 Then
                strType = Trim(Mid(Pstring, intP + strP.Length, InStr(intP, Pstring, ",") - intP - strP.Length))
            Else
                strType = Trim(Mid(Pstring, intP + strP.Length, Pstring.Length - intP - strP.Length + 1))
            End If
            'データ型によって処理を切り替える
            Select Case LCase(Mid(strType, 1, 3))
                Case "cha", "var" '文字列型の場合 char,varchar
                    nP = param3 + strP.Length * 2 + 10
                    nLen = CInt(byteData(nP))
                    param3 = nP + nLen + 2
                    sP = "'" & Encoding.GetEncoding("shift_jis").GetString(byteData, nP + 2, nLen) & "'"
                Case Else '数値型と日付型の場合 int,smallint,float,real
                    nP = param3 + strP.Length * 2 + 4
                    nLen = CInt(byteData(nP))
                    param3 = nP + nLen + 1
                    Select Case LCase(strType)
                        Case "int"
                            sP = CStr(BitConverter.ToInt32(byteData, nP + 1))
                        Case "smallint"
                            sP = CStr(BitConverter.ToInt16(byteData, nP + 1))
                        Case "float"
                            sP = CStr(BitConverter.ToDouble(byteData, nP + 1))
                        Case "real"
                            sP = CStr(BitConverter.ToSingle(byteData, nP + 1))
                        Case "datetime"
                            '日付部分。前半4バイト
                            datecount = BitConverter.ToInt32(byteData, nP + 1)
                            paramDate = DateAdd(DateInterval.Day, datecount, dateTime1)
                            '時刻部分。後半4バイト
                            datecount = BitConverter.ToInt32(byteData, nP + 5) / 300
                            paramDate = DateAdd(DateInterval.Second, datecount, paramDate)
                            'Jetデーターベースエンジンの場合、日付型は#で囲む
                            If IsSQLServer = 0 Then
                                sP = "'" & CStr(paramDate) & "'"
                            ElseIf IsSQLServer = 1 Then
                                'Jetデータベースエンジンにアクセスする場合
                                sP = "#" & CStr(paramDate) & "#"
                            Else
                                sP = "'" & CStr(paramDate) & "'"
                            End If
                        Case Else
                            sP = 0
                    End Select

            End Select
            'パラメータを値を表す文字列に置き換え
            SQLstring = Mid(SQLstring, 1, param1 - 1) & sP & Mid(SQLstring, param1 + strP.Length)
            '次の@を検索
            param1 = InStr(param1 + 1, SQLstring, "@")
        Loop
        '文字列に変換したUPDATE文が戻り値
        UtilSQLPrxyUPDATEwithDataset = Trim(SQLstring)
    End Function

    Private Function JetDBSvrCreateMsgHeader(ByRef byteSendData As Byte(), ByRef jetDBtable As DataTable, ByRef jetDBcolArr() As DataColumn, ByVal strHeader As String) As Integer
        Dim cOffset As Integer
        Dim column As DataColumn
        Dim strColHeader As String
        Dim bytesEncodedCount As Integer
        Dim i As Integer
        Dim keycol As Boolean

        'メッセージヘッダー作成
        cOffset = JetDBSvrString16toBinary(byteSendData, strHeader, 0)
        byteSendData(cOffset + 1) = CType(jetDBtable.Columns.Count, Byte) '列数
        byteSendData(cOffset + 2) = 0
        cOffset = cOffset + 3

        For Each column In jetDBtable.Columns
            keycol = False
            CheckByteArraySize(byteSendData, cOffset, 12)
            '列名ヘッダー作成(キー列でない場合)
            strColHeader = "00 00 09 00 "
            'キーが設定されている場合
            If jetDBcolArr.Length.ToString() > 0 Then
                '最大で、多重キーの多重化分だけループを回る
                For i = 0 To jetDBcolArr.GetUpperBound(0)
                    If column.ColumnName = jetDBcolArr(i).ColumnName Then
                        'この列がキー列の場合、列名ヘッダーが次のようになる。
                        strColHeader = "00 00 08 00 "
                        keycol = True
                        Exit For
                    End If
                Next i
            End If

            cOffset = JetDBSvrString16toBinary(byteSendData, strColHeader, cOffset)
            cOffset = cOffset + 1

            Select Case column.DataType.FullName
                Case "System.String"
                    strColHeader = "A7 FF FF 11 04 D0 00 00 "
                    cOffset = JetDBSvrString16toBinary(byteSendData, strColHeader, cOffset)
                    cOffset = cOffset + 1
                Case "System.Int16"
                    If keycol = False Then
                        byteSendData(cOffset) = CType("&H26", Byte)
                        byteSendData(cOffset + 1) = CType("&H02", Byte)
                        cOffset = cOffset + 2
                    Else 'キー列の場合
                        byteSendData(cOffset) = CType("&H34", Byte)
                        cOffset = cOffset + 1
                    End If
                Case "System.Int32"
                    If keycol = False Then
                        byteSendData(cOffset) = CType("&H26", Byte)
                        byteSendData(cOffset + 1) = CType("&H04", Byte)
                        cOffset = cOffset + 2
                    Else 'キー列の場合
                        byteSendData(cOffset) = CType("&H38", Byte)
                        cOffset = cOffset + 1
                    End If
                Case "System.Int64"
                    If keycol = False Then
                        byteSendData(cOffset) = CType("&H26", Byte)
                        byteSendData(cOffset + 1) = CType("&H08", Byte)
                        cOffset = cOffset + 2
                    Else 'キー列の場合
                        byteSendData(cOffset) = CType("&H7F", Byte)
                        cOffset = cOffset + 1
                    End If
                Case "System.Double"
                    If keycol = False Then
                        byteSendData(cOffset) = CType("&H6D", Byte)
                        byteSendData(cOffset + 1) = CType("&H08", Byte)
                        cOffset = cOffset + 2
                    Else 'キー列の場合
                        byteSendData(cOffset) = CType("&H3E", Byte)
                        cOffset = cOffset + 1
                    End If
                Case "System.Single"
                    If keycol = False Then
                        byteSendData(cOffset) = CType("&H6D", Byte)
                        byteSendData(cOffset + 1) = CType("&H04", Byte)
                        cOffset = cOffset + 2
                    Else 'キー列の場合
                        byteSendData(cOffset) = CType("&H3B", Byte)
                        cOffset = cOffset + 1
                    End If
                Case "System.DateTime"
                    If keycol = False Then
                        byteSendData(cOffset) = CType("&H6F", Byte)
                        byteSendData(cOffset + 1) = CType("&H08", Byte)
                        cOffset = cOffset + 2
                    Else 'キー列の場合
                        byteSendData(cOffset) = CType("&H3D", Byte)
                        cOffset = cOffset + 1
                    End If
            End Select
            'カラム名の長さ
            byteSendData(cOffset) = CType(column.Caption.Length, Byte)
            cOffset = cOffset + 1

            CheckByteArraySize(byteSendData, cOffset, Encoding.Unicode.GetByteCount(column.Caption))
            'カラムネームデータ作成
            bytesEncodedCount = Encoding.Unicode.GetBytes(column.Caption, 0, column.Caption.Length, byteSendData, cOffset)
            cOffset = cOffset + bytesEncodedCount

        Next
        JetDBSvrCreateMsgHeader = cOffset
    End Function
    Private Function JetDBSvrCreateData(ByRef byteSendData As Byte(), ByRef jetDBtable As DataTable, ByRef jetDBcolArr() As DataColumn, ByVal cOffset As Integer) As Integer
        Dim row As DataRow
        Dim column As DataColumn
        Dim i As Integer

        'ここから、取得した結果セットをメッセージに埋め込む
        byteSendData(cOffset) = CType("&HD1", Byte)
        cOffset = cOffset + 1

        Dim Schar() As Char
        Dim byteNum As Byte()
        Dim nstrlen As Integer
        Dim sjisEncoder As Encoder = Encoding.GetEncoding("shift_jis").GetEncoder()
        Dim byteCount As Integer
        Dim bytesEncodedCount As Integer
        Dim strHeaderLen_Offset As Integer
        Dim keycol As Boolean

        For Each row In jetDBtable.Rows
            For Each column In jetDBtable.Columns
                keycol = False
                If jetDBcolArr.Length.ToString() > 0 Then
                    '最大、多重キーの多重化の回数ループ
                    For i = 0 To jetDBcolArr.GetUpperBound(0)
                        If column.ColumnName = jetDBcolArr(i).ColumnName Then
                            keycol = True
                            Exit For
                        End If
                    Next i
                End If

                Select Case column.DataType.FullName
                    Case "System.String"
                        CheckByteArraySize(byteSendData, cOffset, 2)
                        '列ヘッダー
                        strHeaderLen_Offset = cOffset
                        byteSendData(cOffset) = 0 'データの長さはあとで代入する
                        byteSendData(cOffset + 1) = 0
                        cOffset = cOffset + 2
                        If row(column) Is DBNull.Value Then
                            'データがNullの場合、文字数を0に設定しないとエラーが発生する
                            nstrlen = 0
                        Else
                            '通常処理
                            nstrlen = Len(row(column))
                        End If

                        '列データ
                        Schar = row(column)
                        'バイナリでの長さを取得
                        byteCount = sjisEncoder.GetByteCount(Schar, 0, Schar.Length, True)
                        CheckByteArraySize(byteSendData, cOffset, byteCount)
                        '引数に指定したバイナリ配列「byteSendData」に文字列追加
                        bytesEncodedCount = sjisEncoder.GetBytes(Schar, 0, Schar.Length, byteSendData, cOffset, True)

                        cOffset = cOffset + byteCount

                        'データのサイズをデータの前にいれる。
                        byteSendData(strHeaderLen_Offset) = cOffset - strHeaderLen_Offset - 2

                    Case "System.Int16", "System.Int32", "System.Int64", "System.Double", "System.Single"
                        CheckByteArraySize(byteSendData, cOffset, 1)

                        'この列がキー列の場合、データサイズは格納しない
                        If keycol = False Then
                            'この列がキー列でない場合、データサイズを格納
                            byteSendData(cOffset) = CType(Len(row(column)), Byte)
                            cOffset = cOffset + 1
                        End If

                        '数値データの大きさに合わせて配列のサイズを変更
                        ReDim byteNum(Len(row(column)) - 1)
                        '数値データをバイト配列に変換
                        byteNum = BitConverter.GetBytes(row(column))
                        CheckByteArraySize(byteSendData, cOffset, Len(row(column)))
                        'データのバイト数だけ繰り返し、1バイトずつメッセージに追加する
                        For i = 0 To Len(row(column)) - 1
                            byteSendData(cOffset + i) = byteNum(i)
                        Next
                        cOffset = cOffset + Len(row(column))
                    Case "System.DateTime"
                        cOffset = JetDBSvrDateTimetoBinary(byteSendData, row(column), cOffset, keycol)

                End Select
            Next column

            '配列の残りが1024以下だったら2048追加する
            If cOffset + 1024 > UBound(byteSendData) Then
                'byteSendDataの配列の大きさが足りなくなったら再割り当て
                ReDim Preserve byteSendData(cOffset + 2048)
            End If

            CheckByteArraySize(byteSendData, cOffset, 1)
            byteSendData(cOffset) = CType("&HD1", Byte)
            cOffset = cOffset + 1
        Next row
        If CStr(Hex(byteSendData(cOffset - 1))) = "D1" Then
            cOffset = cOffset - 1
        End If
        JetDBSvrCreateData = cOffset
    End Function

    Private Function JetDBSvrString16toBinary(ByRef byteSendData As Byte(), ByVal strdata As String, ByVal cOffset As Integer) As Integer
        Dim i As Integer
        Dim j As Integer
        j = cOffset
        For i = 1 To strdata.Length - 1 Step 3
            byteSendData(j) = CType("&H" & Mid(strdata, i, 2), Byte)
            j = j + 1
        Next
        JetDBSvrString16toBinary = j - 1
    End Function
    Private Sub JetDBSvrConvIntToByte(ByRef byteSendData As Byte(), ByRef row As DataRow, ByRef column As DataColumn, ByVal dimcount As Integer)
        Dim nval1, nval2 As Int64
        Dim str As String = ""
        Dim nval As Int64 = 256
        Dim i, j As Integer

        nval1 = row(column)
        If nval1 < 0 Then
            Select Case column.DataType.FullName
                Case "System.Int16" : nval = nval * nval
                Case "System.Int32" : nval = nval * nval * nval * nval
                Case "System.Int64" : nval = nval * nval * nval * nval * nval * nval * nval * nval
            End Select
            nval1 = nval + nval1
        End If

        Do While nval1 >= 256
            nval2 = nval1 Mod 256
            nval1 = nval1 \ 256
            If Hex(nval2).Length = 1 Then
                str = str & "0" & Hex(nval2) & " "
            Else
                str = str & Hex(nval2) & " "
            End If
        Loop
        If Hex(nval1).Length = 1 Then
            str = str & "0" & Hex(nval1)
        Else
            str = str & Hex(nval1)
        End If
        j = dimcount
        For i = 1 To str.Length Step 3
            byteSendData(j) = CType("&H" & Mid(str, i, 2), Byte)
            j = j + 1
        Next
    End Sub
    Private Function JetDBSvrDateTimetoBinary(ByRef byteSendData As Byte(), ByVal row_column As Date, ByVal cOffset As Integer, ByVal keycol As Boolean) As Integer
        Dim byteNum As Byte()
        Dim datecount As Integer
        Dim dateTime1 As Date = #1/1/1900#
        Dim i As Integer

        CheckByteArraySize(byteSendData, cOffset, 9)
        If keycol = False Then
            '列ヘッダー
            byteSendData(cOffset) = CType("&H08", Byte) 'データの長さ
            cOffset = cOffset + 1
        End If
        '列データ
        datecount = DateDiff(DateInterval.Day, dateTime1, row_column)
        ReDim byteNum(3)
        byteNum = BitConverter.GetBytes(datecount)
        For i = 0 To 3
            byteSendData(cOffset + i) = byteNum(i)
        Next
        cOffset = cOffset + 4
        datecount = ((Hour(row_column) * 3600) + (Minute(row_column) * 60) + Second(row_column)) * 300
        ReDim byteNum(3)
        byteNum = BitConverter.GetBytes(datecount)
        For i = 0 To 3
            byteSendData(cOffset + i) = byteNum(i)
        Next

        cOffset = cOffset + 4
        JetDBSvrDateTimetoBinary = cOffset
    End Function

    Private Sub CheckByteArraySize(ByRef byteSendData As Byte(), ByVal cOffset As Integer, ByVal addsize As Integer)
        If cOffset + addsize > UBound(byteSendData) Then
            'byteSendDataの配列の大きさが足りなくなったら再割り当て
            ReDim Preserve byteSendData(cOffset + addsize)
        End If

    End Sub

    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, _buffer_size, 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
        If Not customize_flg Then '「カスタマイズ」チェックボックス
            'カスタマイズ用イベントプロシージャの呼び出し
            byteData = AppSQLPrxyReceiveFromClient(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 AppOutputDataSQLDB()
        Me.txtSQLDB.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 _OutputDataSQLDB(ByVal strDisp As String)
        DispDataBufSQL = strDisp
        Me.Invoke(New AsyncServerDelegate(AddressOf AppOutputDataSQLDB))
    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)
        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

