Spurious responses to socket open request

I’m working with an EC21-E module. I need to open a TCP connection to a remote server and the procedure works when the module is clear (immediately after reset, no previous connections). If I reset my application without powering down the module and try to use the same connection id the QIOPEN command obviously complains about it (error 563); my fallback procedure is then to close the socket and try to open the next one for good measure. For example, when requesting to open socket id 0 and seeing that it is already open I close it and retry with socket id 1.

This mostly works except sometimes the module randomly responds by saying that another socket is open. For example, the conversation will go as follows:

AT+QIOPEN=1,3,"TCP","185.3.37.170",3400,0,1
OK

+QIOPEN: 2,563

AT+QICLOSE=2
AT+QICLOSE=3

OK

+QIOPEN: 2,563

Sometimes it gets stuck in this loop where it continuously reports socket 2 to be already opened even though I’m trying to close it. Other times after requesting to close socket 2 it will then report that socket 3 was actually successfully opened. I’ve tried a few variations of the open/close commands but it seems that every time a socket is already opened there is no way to close it and start over.

I don’t understand what’s the reasoning here. If I try to open socket 3, why would I care that socket 2 is already opened? And why is it unable to close it?

Hi,
Application should wait for response 150s at most after it opens a socket with command AT+QIOPEN. The concrete time is up to the network situation. If open socket is success, application will receive URC +QIOPEN: 0,0.

In log you shared. the connect ID 2 may be opened previously. You can use AT+QISTATE to query all opened socket.