I think you have it wrong. The fact that it sticks inside the accept is a good thing. If it gets out it means that an error occurred or the node on the other side has cancelled the session.
here's how I work it.
I recycle two handles because my host connection will occasionaly close the session and re-open another.
I am also making sure that the ip address of the node always remains the same as the original connection to prevent someone taking over the session.
while(socket_flag == TRUE) { sock2 = accept(listensock, &raddr, &addr_length); return_error_if(sock2 == RTCS_SOCKET_ERROR); if(addr.sin_addr.s_addr == raddr.sin_addr.s_addr) { shutdown(sock, FLAG_CLOSE_TX); fclose(sockfd); sockfd = fopen("socket:", (char_ptr)sock2); socket_flag = TRUE; } else { diag_led_red(); RTCS_time_delay(100); continue; } sock = accept(listensock, &addr, &addr_length); return_error_if(sock == RTCS_SOCKET_ERROR); if(addr.sin_addr.s_addr == raddr.sin_addr.s_addr) { shutdown(sock2, FLAG_CLOSE_TX); fclose(sockfd); sockfd = fopen("socket:", (char_ptr)sock); socket_flag = TRUE; } else { diag_led_grn(); RTCS_time_delay(100); continue; } }//while(socket_flag == TRUE)