Option Explicit
Dim blnClientConnected(7) As Boolean

Private Sub cmdColor_Click()
dlgChat.Flags = cdlCCRGBInit
dlgChat.Color = txtMessage.ForeColor

dlgChat.ShowColor

lstConversation.BackColor = dlgChat.Color

End Sub

Private Sub cmdConnect_Click()
If txtHost.Text <> "" And txtPort.Text <> "" Then
If optServer.Value = True Then 'Server
    Dim i As Integer
        For i = 0 To 7
            wnsckClient(i).LocalPort = Val(txtPort.Text) + i
            wnsckClient(i).Listen
            lstConversation.AddItem "Listening to Port: " & Val(txtPort.Text) + i
        Next i
Else 'Client
    wnsckConnect.RemoteHost = txtHost.Text
    wnsckConnect.RemotePort = txtPort.Text
    wnsckConnect.Connect
    
End If
Else
MsgBox "Please Enter the Host And Port number then select mode then connect", vbOKOnly + vbInformation, "iCHAT"
End If

End Sub

Private Sub cmdFont_Click()
dlgChat.Flags = cdlCFScreenFonts
dlgChat.FontName = txtMessage.FontName
dlgChat.FontBold = txtMessage.FontBold
dlgChat.FontItalic = txtMessage.FontItalic
dlgChat.FontSize = txtMessage.FontSize

dlgChat.ShowFont

txtMessage.FontName = dlgChat.FontName
txtMessage.FontBold = dlgChat.FontBold
txtMessage.FontItalic = dlgChat.FontItalic
txtMessage.FontSize = dlgChat.FontSize

End Sub

Private Sub cmdForeColor_Click()
dlgChat.Flags = cdlCCRGBInit
dlgChat.Color = txtMessage.ForeColor

dlgChat.ShowColor

txtMessage.ForeColor = dlgChat.Color

End Sub

Private Sub cmdMusic_Click()
Form2.Show

End Sub

Private Sub cmdSearch_Click()
Dim strPath As String

strPath = App.Path


    If strPath <> "\" Then
        strPath = strPath & "\IEXPLORER.exe"
    Else
        strPath = strPath & "IEXPLORER.exe"
    End If
    Shell ("C:\Program Files\Internet Explorer\IEXPLORE.exe")
    
End Sub

Private Sub cmdSend_Click()
Dim tempMessage As String
Dim i As Integer

tempMessage = txtUName.Text & ": " & txtMessage.Text
If optServer.Value = True Then 'server
    For i = 0 To 7
        If blnClientConnected(i) = True Then
            lstConversation.ForeColor = dlgChat.Color
            wnsckClient(i).SendData tempMessage
        End If
    Next i
Else 'client
    lstConversation.ForeColor = dlgChat.Color
    wnsckConnect.SendData tempMessage
End If

txtMessage.Text = ""
txtMessage.SetFocus
        
End Sub

Private Sub cmdSend_GotFocus()
cmdSend.BackColor = &H8000000D

End Sub

Private Sub Command6_Click()
cmdSend.BackColor = &H8000000F
Unload frmChat

End Sub

Private Sub Command6_GotFocus()
Command6.Width = 1200
Command6.Height = 450
Command6.BackColor = &H8000000D

End Sub

Private Sub Form_Load()
Dim i As Integer
    For i = 0 To 7
        blnClientConnected(i) = False
    Next i

End Sub



Private Sub Form_Unload(Cancel As Integer)
Dim intUserResp As String

intUserResp = MsgBox("Do you want to exit?", vbYesNo, "Chatroom")

If intUserResp = vbNo Then
    Cancel = 1
End If

End Sub

Private Sub ShockwaveFlash1_Click()

End Sub


Private Sub wnsckClient_ConnectionRequest(Index As Integer, ByVal requestID As Long)
'server
If wnsckClient(Index).State <> sckClosed Then
   wnsckClient(Index).Close
End If

wnsckClient(Index).Accept requestID
lstConversation.AddItem "User Connected "
blnClientConnected(Index) = True

'now send this to other clients
Dim i As Integer
    For i = 0 To 7
        If blnClientConnected(i) = True Then
            wnsckClient(i).SendData "user connected"
        End If
    Next i
        
End Sub

Private Sub wnsckClient_DataArrival(Index As Integer, ByVal bytesTotal As Long)
'server code
Dim receivedData As String
wnsckClient(Index).GetData receivedData, vbString
lstConversation.Font = txtMessage.FontName
lstConversation.AddItem receivedData, 0

'now send to the other clients
Dim i As Integer
    For i = 0 To 7
        If blnClientConnected(i) = True Then
            lstConversation.Font = txtMessage.FontName
            wnsckClient(i).SendData receivedData
        End If
    Next i
    
End Sub

Private Sub wnsckClient_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox ("ERROR!" & vbCrLf & Description)

End Sub

Private Sub wnsckConnect_ConnectionRequest(ByVal requestID As Long)
If wnsckConnect.State <> sckClosed Then
    wnsckConnect.Close
End If
wnsckConnect.Accept requestID

End Sub

Private Sub wnsckConnect_DataArrival(ByVal bytesTotal As Long)
Dim receivedData As String
wnsckConnect.GetData receivedData, vbString
lstConversation.Font = txtMessage.FontName
lstConversation.AddItem receivedData, 0

End Sub

