Some operations take a long time to execute, mostly because they involve talking to another host, which may have an exruciating long timeout on it. If you don't want to block your UI while performing this operation you can either use threads, or non blocking sockets.
For instance, when doing a connect(2) on a nonblocking socket, it will immediately return EINPROGRESS to let you know that the operation is in progress, and when select(2) (or related interface) says that the socket is ready for writing, you can call getsockopt(fd, SOL_SOCKET, SO_ERROR, ...) on it. getsockopt(2) will return with the error connect(2) would have indicated, had it been called in a blocking manner. If the error is 0, the connect succeeded.