    DIM SHARED Host AS LONG
     
    SCREEN _NEWIMAGE(800, 600, 32)
    COLOR &HFFFFFFFF, &HFF000000
    Host = _OPENHOST("TCP/IP:7990") ' this will be the host code
     
    DO
        Player = GetClient 'Not
        IF Player THEN
            PRINT "New Player connected"
            'Do stuff
            UserData$ = In$(Player)
            'do stuff with the data the user sent
     
            'and close the connection
            CLOSE Player
            Player = 0
        END IF
        _LIMIT 30
    LOOP
     
     
    FUNCTION GetClient
        GetClient = _OPENCONNECTION(Host) ' receive any new connection
    END FUNCTION
     
    FUNCTION In$ (who)
        DIM b AS _UNSIGNED _BYTE
        'CHR$(2) = Start of Text
        'CHR$(3) = End of Text
        'CHR$(4) = End of Transmission (It's what we use to tell the client, "We give up!  Closing connection!"
        'CHR$(6) = Acknowledge
        'CHR$(15) = Not Acknowledge
     
        GET #who, , b 'just check for a single byte from each connection
     
        IF b <> 2 THEN
            'If we get something which isn't a CHR$(2) to start communication, send back a failure notice.
            SendError who
            EXIT FUNCTION 'Exit so we can move on to the next connection to check for that leading chr$(2)
        END IF
     
        'Only if that initial byte is CHR$(2), do we acknowledge receipt and await further messages.
     
        SendConfirmation who 'we send ACKnowledgement back to tell the client we're ready for them to talk to us.
     
        DO
            count = count + 1
            timeout## = ExtendedTimer + 5
            DO
                _LIMIT 100 'no need to check for incoming information more than 100 times a second!
                GET #who, , a$
                IF a$ <> "" THEN tempIn$ = tempIn$ + a$ 'tempIn$ should never be more than 105 bytes
                ETX = INSTR(a$, CHR$(3)) '       chr$(3) is our ETX character (End of TeXt)
                IF ETX THEN EXIT DO
                IF ExtendedTimer > timeout## THEN 'If it takes over 5 seconds to send 100 bytes (or less) of info
                    SendError who '              something is wrong.  Terminate the attempt, but be nice, and let
                    EXIT FUNCTION '              the other client know something went wrong, so they can try again,
                END IF '                         if they want to.
            LOOP UNTIL LEN(tempIn$) > 105 'If we have over 105 bytes with our string, we didn't send the data properly.
            IF LEN(tempIn$) > 105 THEN
                SendError who 'send the client an error message
                EXIT FUNCTION
            END IF
            tempIn$ = _TRIM$(LEFT$(tempIn$, ETX - 1)) 'strip off the ETX character and check to make certain data is valid.
     
            c$ = RIGHT$(tempIn$, 4) 'these 4 bytes are the checksum
     
            CheckSum = CVL(c$) 'Check to make certain the data apprears valid.
            FOR i = 1 TO LEN(l$): Check = Check + ASC(l$, i): NEXT
            IF CheckSum <> Check THEN '            Our data is not what we expected.  Part may be lost, or corrupted.
                SendError who
                EXIT FUNCTION
            ELSE
                SendConfirmation who
                EXIT DO
            END IF
        LOOP UNTIL count = 5
        'If we get bad data 5 times in a row, something is wrong.  We're just going to close the connection.
        IF count = 5 THEN
            SendError who
            EXIT FUNCTION
        END IF
     
        'and if we're down this far, our data has been recieved, verified, and is now good to use.
        In$ = LEFT$(tempIn$, 4) 'left part of the string is the data the user is sending us
    END FUNCTION
     
    SUB SendError (who)
        DIM b AS _UNSIGNED _BYTE
        b = 4
        PUT #who, , b
    END SUB
     
    SUB SendConfirmation (who)
        DIM b AS _UNSIGNED _BYTE
        b = 6
        PUT #who, , b
    END SUB
     
    FUNCTION ExtendedTimer##
        DIM m AS INTEGER, d AS INTEGER, y AS INTEGER
        DIM s AS _FLOAT, day AS STRING
        day = DATE$
        m = VAL(LEFT$(day, 2))
        d = VAL(MID$(day, 4, 2))
        y = VAL(RIGHT$(day, 4)) - 1970
        SELECT CASE m 'Add the number of days for each previous month passed
            CASE 2: d = d + 31
            CASE 3: d = d + 59
            CASE 4: d = d + 90
            CASE 5: d = d + 120
            CASE 6: d = d + 151
            CASE 7: d = d + 181
            CASE 8: d = d + 212
            CASE 9: d = d + 243
            CASE 10: d = d + 273
            CASE 11: d = d + 304
            CASE 12: d = d + 334
        END SELECT
        IF (y MOD 4) = 2 AND m > 2 THEN d = d + 1 'add a day if this is leap year and we're past february
        d = (d - 1) + 365 * y 'current month days passed + 365 days per each standard year
        d = d + (y + 2) \ 4 'add in days for leap years passed
        s = d * 24 * 60 * 60 'Seconds are days * 24 hours * 60 minutes * 60 seconds
        ExtendedTimer## = (s + TIMER)
    END FUNCTION
     
