SendCommand       PROC    reqOP:DWORD, dataPTR:DWORD, usableSOCKET:DWORD

                  ; 1. calcolare la LUNGHEZZA TOTALE DEL BUFFER IN INGRESSO

                  .IF dataPTR!=NULL
                      INVOKE  StrLen, dataPTR
                  .ELSE
                      xor     eax,eax
                  .ENDIF


                  ; 2. calcolare il numero di pacchetti da 1024bytes contenuti (uno shr)

                  shr     eax, 10
                  inc     eax                        ;uno in + per il controllo
                  mov     packet.nrPacketInSeq, eax

                  ; 3. loop interno -> fill del packetBUFFER e invio (.WHILE eax<nrPacketInSeq, dove eax=nrPacketSeqNR)

                  xor     eax, eax

                  .WHILE  eax<packet.nrPacketInSeq
                          
                          push    eax

                          mov     packet.nrPacketSeqNR, eax       ; nr. del pacchetto nella sequenza

                          mov     eax, reqOP                      ; REQUESTED OPERATION CODE
                          mov     packet.nrFCode, eax
                         
                          pop     eax
                          push    eax


                          .IF dataPTR==NULL
                              mov     packet.nrLenData, 0
                              mov     packet.nrPointData, NULL
                              mov     esi, OFFSET packetENDSEQ
                              mov     edi, OFFSET packet.nrTail
                              movsd
                          .ELSE
                               mov     ebx, packet.nrPacketInSeq
                               dec     ebx

                               .IF eax==ebx 
                                   mov      ebx, eax                   ; lunghezza dei dati e offset nel dataPTR
                                   .IF ebx==0                          ; pare pacchetto unico
                                       INVOKE   StrLen, dataPTR
                                       mov      packet.nrLenData, eax
                                       mov      eax, dataPTR
                                       mov      packet.nrPointData, eax
                                   .ELSE
                                        mov      ebx, dataPTR

                                        shl      eax, 10
                                        add      ebx, eax
                                        mov      inSendBuffer, ebx
                                        INVOKE   StrLen, inSendBuffer
                                        mov      packet.nrLenData, eax
                                        mov      ebx, dataPTR
                                        mov      eax, packet.nrPacketInSeq
                                        dec      eax
                                        shl      eax, 10
                                        add      ebx, eax
                                        mov      packet.nrPointData, ebx
                                   .ENDIF

                                   mov      esi, OFFSET packetENDSEQ   ; tail del pacchetto = END SEQUENCE
                                   mov      edi, OFFSET packet.nrTail
                                   movsd

                               .ELSE
                                    mov     packet.nrLenData, 1024     ; lunghezza dei dati e offset nel dataPTR
                                    shl     eax, 10
                                    mov     ebx, dataPTR
                                    add     ebx, eax
                                    mov     packet.nrPointData, ebx

                                    mov     esi, OFFSET packetNEXTSEQ ; tail del pacchetto = STANDARD IN-SEQ packet
                                    mov     edi, OFFSET packet.nrTail
                                    movsd
                               .ENDIF                          
                          .ENDIF

                          ; -- Filling del packetBUFFER

                          mov     edi, OFFSET packetBUFFER
                          mov     esi, OFFSET packet.nrHeader
                          movsd
                          mov     esi, OFFSET packet.nrFCode
                          movsd

                          mov     esi, OFFSET packet.nrPacketSeqNR
                          movsd
                          mov     esi, OFFSET packet.nrPacketInSeq
                          movsd

                          .IF dataPTR==NULL 
                              xor      eax, eax
                              stosd
                              stosd
                          .ELSE
                               mov     esi, OFFSET packet.nrLenData
                               movsd
                               mov     esi, packet.nrPointData
                               mov     ecx, packet.nrLenData
                               rep     movsb
                          .ENDIF

                          mov     esi, OFFSET packet.nrTail
                          movsd

                          ; -- Time to send our stuff

                          mov     eax, packet.nrLenData
                          add     eax, 4+4+4+4+4+4

                          INVOKE  send, usableSOCKET, ADDR packetBUFFER, eax, 0

                          .IF eax==0 || eax==SOCKET_ERROR
                              INVOKE  ClearBuffer, ADDR packetBUFFER, 1024+24
                              ret
                          .ENDIF

                          ; -- clear buffer

                          INVOKE  ClearBuffer, ADDR packetBUFFER, 1024+24

                          ; -- Altro giro altro pacchetto (regalo)

                          pop     eax
                          inc     eax
                  .ENDW

                  ret
SendCommand       ENDP