SOCKETSTATE_CONNECTED and SOCKETSTATE_DISCONNECTED are set by URC handler while SOCKETSTATE_CONNECTING is set by the cellular API, which is in user thread. If the URC set the socket state earlier, the socket state will be overwritten by cellular API.
Move the line set socket CONNECTING state forward. No socket connected URC will be received before sending socket connect AT command.
Revert the state back to ALLOCATED if modem returns error.
SOCKETSTATE_CONNECTED and SOCKETSTATE_DISCONNECTED are set by URC handler while SOCKETSTATE_CONNECTING is set by the cellular API, which is in user thread. If the URC set the socket state earlier, the socket state will be overwritten by cellular API.