I can open a socket fine, connect, and send data perfectly. But if I power cycle the system, when it boots up again and tries to open a connection, it will wait a long time in connect(). After it fails, I have it close that socket, open a new one, and it connects just fine. How can I close that socket that I lost when I lost power?
Interestingly, if I then power cycle the system again, it connects first try. If I power cycle it again after that, it waits around for connect to fail, and then connects second try. The code to connect both times is the same code, so its not an issue there.
In addition, if I have the server close the socket, my code detects that, closes it on my end, waits 1 second, and reconnects. This works fine, too.
I think that the reason that it doesn't connect properly the second time is because it is trying to use the old socket that wasn't closed properly. When it fails, it gets a new socket that works. I tried to force this by opening a socket, closing it, and opening another. This changes nothing.
Is there some time I should wait after I call closesocket() before I try to open another one? Or is there a way to force the CC3000 to give me good clean socket? None of this would be a problem either if I could reduce the connect() timeout. My solution right now is just have a watchdog timer come and reset the system if connect is taking too long.