#define EXTCHAR Chr(255)
ScreenRes 320, 200,32
Color RGB(81, 155, 55)
    #include "win/winsock2.bi"
    Dim wsData as wsaData
    Dim wVer as long
    Dim errorCheck as integer
    Dim Shared as socket ClientSocket
    Dim NetworkInfo as sockaddr_in

    wVer = makeword(1,1)

    if WSAStartup ( wVer, @wsData) = SOCKET_ERROR then
       print "Error: ", wsaGetLastError()
       wsaCleanUp()
       End
    else
       print "ok"
    end if

    ClientSocket = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )

    if ClientSocket = INVALID_SOCKET then
       print "Error: ", wsaGetLastError()
    else
       print "ok"
    end if

    with NetworkInfo
        .sin_family      = AF_INET
        .sin_port        = htons( 9999 )
        .sin_addr.S_addr = inet_addr("127.0.0.1")
    end With

    connect(ClientSocket,cast(PSOCKADDR,@NetworkInfo),sizeof(NetworkInfo))
    
    If wsaGetLastError()=10061 Then 
     	Print "Error ss:" & wsaGetLastError()
    CloseSocket(ClientSocket)
    WSACleanup
    Sleep
    end
     Else
     	Print "Connected : last error:" & wsaGetLastError()
     EndIf
     
     
     
 Dim as zstring*316 outmessage
 Dim As String k
 
 Dim Shared messages(1 To 2000) As String
 Dim Shared As Integer messages_count,messages_max=2000 , x ,y=150
 

'messages
Sub threadx()
dim as zstring*316 nn

Dim As Byte bytes
bytes = recv( ClientSocket, @nn , 316, 0 )
        If nn<>"" Then
          messages_count+=1
          messages(messages_count)=nn
          y-=10
        EndIf
End Sub
Dim As ulong iMode=1 : IOCtlSocket(CLIENTSocket, FIONBIO, @iMode) 'SET NON-BLOCKING
Dim As Any Ptr n = ImageCreate( 320,20):BLoad "n.bmp",n
Dim As Any Ptr g = ImageCreate( 320,200):BLoad "g.bmp",g

Sleep 1000
cls
Dim Shared As String namef
Do
Input "Insert your name: ", namef
Loop Until namef<>""
'send ur name
dim as zstring*316 na
na="name:" & namef
send(ClientSocket,@na,316,0)



Cls

    do
      
     k = InKey
      
      
   If k=EXTCHAR & "H" Or k=EXTCHAR & "P" Then
   Else
   If (k)<>"" Or asc(k)<>13 Then outmessage+=k
   endif 
      
       If Asc(k)=13 Then
       messages_count+=1
       outmessage=Left(outmessage,Len(outmessage)-1)
       messages(messages_count)="YOU:" & outmessage
       y-=10
       outmessage=namef & ":" & (outmessage)
       send(ClientSocket,@outmessage,316,0)
       outmessage=""
       EndIf

threadx()

If asc(k)=27 Then 
	dim as zstring*316 dis
	dis="exit"
	send(ClientSocket,@dis,316,0)
Print "EX"
Exit Do
EndIf   
    Sleep 10
    
    ScreenLock
    Cls
    Put (0,0),g,trans 
    Put (0,165),n,trans 
    Draw String (0, 170), "YOU:" & outmessage

    For i As Integer=1 To messages_count
If y+(i*10)<160 And y+(i*10)>0 Then Draw String (x, y+(i*10)), messages(i)
    Next

    screenunlock
If k=EXTCHAR & "H" Then y+=10
If k=EXTCHAR & "P" Then y-=10
   
    Loop
    closesocket(ClientSocket)
    WSACleanup
    Print "disconnected"
    Sleep 1000,1
    end
