Private Sub btnpreconto_Click
    precontoswitch = 1
If connected = False Then
        connected = True
        If connected Then
            ip=txtipserver.Text
            port = txtport.Text
            CltSock.Initialize("Client")
            CltSock.Connect(ip,port,20000)
            Log("Connesso con preconto in WiFi " & ip & " " & port & " " )
        End If
    Log("Connected " & connected & " chiamo la sub invio ")
                
    else If connected = True Then
        If connected Then
            ip=txtipserver.Text
            port = txtport.Text
            CltSock.Initialize("Client")
            CltSock.Connect(ip,port,20000)
            Log("Connesso in WiFi " & ip & " " & port & " " )
        End If
    Log("Connected " & connected & " chiamo la sub invio ")
    
End If
    
    Log("sub btnpreconto tavolo " & txttavolo.Text)
    
End Sub
Sub Client_Connected(ConStatus As Boolean)
    
    If ConStatus = True Then
        ToastMessageShow("Connesso con wifi su IP " & ip & " Porta " &  port,True)
        AStreams.InitializePrefix(CltSock.InputStream, False, CltSock.OutputStream, "AStreams")
        If wifi2.isWifiEnabled = True Then
            If precontoswitch = 1 Then ' se precontoswitch è 1 allora uso trasmettipreconto
                
                Log("sub Client_Connectedt if tavolo " & txttavolo.Text)
                trasmettipreconto '
                Else
                    
                    btntrasmetti_Click ' if pressed the printer print perfect and then press print another
                Log( "client_connected sta trasmettendo a btntrasmetti_Click")
                Log("sub Client_Connected else tavolo " & txttavolo.Text)
            End If
            
            
            
        Else
            mp2.Load(File.DirAssets,"alert.wav")
            mp2.Play
        MsgboxAsync("Non sei connesso con WiFi, controlla se sei connesso", " ATTENZIONE !!")
            
            End If
    End If
    Log("sub Client_Connected end tavolo " & txttavolo.Text)
End Sub
Sub trasmettipreconto
    
    If connected  Then
        Dim SendMsg As String
        If AStreams.IsInitialized = False Then Return
        Dim buffer() As Byte
        buffer = txttavolozza.Text.GetBytes("UTF8") 'Testosend lo realizzata per inviare tutta la comanda dentro questa casella di testo per essere trasmessa sul buffer di asyncstream.
        Log("TXTTAVOLOZZA =: " & txttavolozza.Text)
        AStreams.Write(buffer)
        txttavolozza.SelectAll
        If chfont.Checked = True Then
            SendMsg = Chr(27) & "!" &Chr(8+8+16) & "" & CRLF
            AStreams.Write(SendMsg.GetBytes("UTF8"))
        End If
         StampaTest
    End If
    
    precontoswitch = 0
End Sub
Sub StampaTest
    Log("sono sulla sub stampatest")
    
    If connected  Then
        Log("sono sulla sub stampatest dentro if connected")
        If AStreams.IsInitialized = False Then Return
        Dim buffertest() As Byte
        Dim sendMsgtest As String
        AStreams.Write(buffertest)
        sendMsgtest=Chr(29) & Chr(86) & Chr(48)' Cut?
        AStreams.Write(sendMsgtest.GetBytes("UTF8"))
        Log("sub stampatest tavolo " & txttavolo.Text)
        
    Else
        
        Log("sono sulla sub stampatest sull'else")
    End If
End Sub