TCP send receive issue in while loop in s7g2

Hi,

I am using DK board for s7g2,i have issue for tcp send receive are not getting properly sync in while loop for Modbus query response structure.I have attached  a screenshot in which you can see that randomly it stuck in receive function 3 or 4 times and then response is getting receive from modbus client

If anyone knows what is happening please share,

UINT tcp_modbus()
{
UINT status = 0;
ULONG bytes_copied;
int var, i;

tx_thread_sleep (2);
status = nx_tcp_socket_receive (&socket_echo, &packet_ptr2, NX_WAIT_FOREVER);

if (status)
{
/* most likely got disconnected */
sock_server_delete ();
do
{
status = nx_tcp_server_socket_accept (&socket_echo, NX_WAIT_FOREVER);
connected = 1;
}
while (status != NX_SUCCESS);
}
else
{
tx_thread_sleep (2);
status = nx_packet_data_retrieve (packet_ptr2, &tx2[0], &bytes_copied);
if (status)
{
/* most likely got disconnected */
sock_server_delete ();
do
{
status = nx_tcp_server_socket_accept (&socket_echo, NX_WAIT_FOREVER);
connected = 1;
}
while (status != NX_SUCCESS);
}
memset(packet_ptr2,0,sizeof(*packet_ptr2));
nx_packet_release(packet_ptr2);

if ((tx2[0] == 1) && (bytes_copied == 8))
{
crc = calculate_crc (tx2, 6);
if ((tx2[6] == (uint8_t) crc) && (tx2[7] == crc >> 8u))
{
if (tx2[1] == 3)
{
reg_start = (uint16_t) ((tx2[2] << 8) | (uint16_t) tx2[3]);
reg_range = (uint16_t) ((tx2[4] << 8) | (uint16_t) tx2[5]);
reg_range_total = reg_start + reg_range;
tx_thread_sleep (5);

resp[0] = 1;
resp[1] = 3;
resp[2] = (reg_range * 2);

var = 3;
for (; reg_start < reg_range_total; reg_start++)
{
resp[var] = (uint8_t) (RS_485_READ[reg_start] >> 8u);
var++;
resp[var] = (uint8_t) (RS_485_READ[reg_start]);
var++;
}
crc = calculate_crc (&resp[0], (reg_range * 2) + 3);

resp[var] = (uint8_t) crc;
var++;
resp[var] = crc >> 8u;
var++;

status = nx_packet_allocate (&g_packet_pool0, &packet_ptr, NX_TCP_PACKET, NX_WAIT_FOREVER);

/* Determine if there is a packet */
if (status == NX_SUCCESS)
{
nx_packet_data_append (packet_ptr, &resp[0], 80, &g_packet_pool0, NX_WAIT_FOREVER);
status = nx_tcp_socket_send(&socket_echo, packet_ptr, NX_WAIT_FOREVER);
if (status)
{
nx_packet_release(packet_ptr);
sock_server_delete ();
do
{
status = nx_tcp_server_socket_accept (&socket_echo, NX_WAIT_FOREVER);
connected = 1;
}
while (status != NX_SUCCESS);
}
}
else
{
nx_packet_release(packet_ptr);
}
nx_packet_release(packet_ptr);
}
else if (tx2[1] == 6)
{
reg_update = (uint16_t) ((tx2[2] << 8) | (uint16_t) tx2[3]);
reg_value = (uint16_t) ((tx2[4] << 8) | (uint16_t) tx2[5]);

RS_485_READ[reg_update] = (uint8_t) (reg_value >> 8u);
RS_485_READ[reg_update] = (uint8_t) (reg_value);

status = nx_packet_allocate (&g_packet_pool0, &packet_ptr, NX_TCP_PACKET, NX_WAIT_FOREVER);

/* Determine if there is a packet */
if (status == NX_SUCCESS)
{
nx_packet_data_append (packet_ptr, tx2, sizeof(tx2), &g_packet_pool0, NX_WAIT_FOREVER);
status = nx_tcp_socket_send(&socket_echo, packet_ptr, NX_WAIT_FOREVER);
if (status)
{
nx_packet_release(packet_ptr);
sock_server_delete ();
do
{
status = nx_tcp_server_socket_accept (&socket_echo, NX_WAIT_FOREVER);
connected = 1;
}
while (status != NX_SUCCESS);
}
}
else
nx_packet_release(packet_ptr);
}
else
{
;
}
}
}
memset (&status, '\0', sizeof(status));
}

return NX_SUCCESS;
}