0X00002748

WSAEISCONN 0X00002748 fix: connecting an already connected socket

Network & Connectivity Intermediate 👁 0 views 📅 May 27, 2026

This error means your app tried to open a second connection on a socket already in use. Close the socket first, then reconnect.

You made a connect request on a socket that's already connected

That's the whole thing. The WSAEISCONN error (0X00002748) pops up when your code or some network service tries to create a new connection using a socket that is still in a connected state. It's a logic bug in how the socket lifecycle is managed. Let's fix it.

The direct fix: close the socket before reconnecting

The most common scenario: you have a loop that tries to reconnect, or your application reuses a socket variable without resetting it. Here's what needs to happen in order:

  1. Check if the socket is valid — don't assume it's closed just because you called closesocket() earlier. Some code paths skip that.
  2. Call closesocket() on the old socket — this releases the socket handle and frees its resources.
  3. Call socket() to create a fresh socket — never reuse the same SOCKET value after closing it. Create a new one.
  4. Call connect() on the new socket — now with a clean slate.

Here's a minimal example in C++ that would trigger the error:

SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server = {};
server.sin_family = AF_INET;
server.sin_port = htons(8080);
inet_pton(AF_INET, "192.168.1.10", &server.sin_addr);

connect(s, (struct sockaddr*)&server, sizeof(server));  // first connect works
connect(s, (struct sockaddr*)&server, sizeof(server));  // WSAEISCONN here

After the first connect() succeeds, the socket s is in a connected state. The second connect() call triggers error 0X00002748. The fix is either:

  • Call closesocket(s) and then socket() again before the second connect.
  • Or use a separate socket for each connection attempt.

Why this happens under the hood

A TCP socket has internal states. After connect() returns successfully (or even if it fails with WSAEWOULDBLOCK), the socket transitions to a connecting or connected state. Windows won't let you call connect() again on that same socket until you close it and create a new one. The socket descriptor is tied to that one connection. You can't reuse it like a throwaway variable.

This is different from some other socket errors like WSAENOTCONN (socket not connected) or WSAEALREADY (a non-blocking connect is in progress). WSAEISCONN specifically means the socket is already fully connected and you tried to connect again.

Less common variations of the same problem

1. Forgetting to check the return value of connect()

Some programmers call connect() in a loop and ignore failures, then try again. If the first connect() actually succeeded, the subsequent attempt triggers WSAEISCONN. Always check the return value.

2. Using select() or WSAEventSelect with a connected socket

You might think you're handling a new connection, but your event handler fires for the same socket twice. The second time you call connect(), you get the error. Solution: set a flag after the first connect, or close the socket in the event handler after the connection completes.

3. Non-blocking connect with WSAEWOULDBLOCK

When you set a socket to non-blocking and call connect(), it often returns SOCKET_ERROR with WSAEWOULDBLOCK. That doesn't mean the connection failed — it's in progress. If you then call connect() again before the connection completes, you'll get WSAEISCONN if it finished, or WSAEALREADY if it's still pending. The rule: never call connect() twice on the same non-blocking socket. Use select() or WSAEventSelect to wait for completion.

4. Multithreaded access to the same socket

Two threads both try to connect the same socket. Thread A connects successfully. Thread B then tries to connect the same socket and gets WSAEISCONN. Protect the socket handle with a mutex, or better, give each thread its own socket.

How to prevent this from happening again

Prevention is straightforward once you know the rules. Here's a short checklist I've used with my team for years:

  • Never reuse a socket handle after connect(). Treat each connection as a disposable object.
  • Always close sockets explicitly with closesocket() and set the SOCKET variable to INVALID_SOCKET afterward. That makes accidental reuse crash early instead of silently failing.
  • Use a state machine if you're doing non-blocking sockets. Track whether you've called connect(), whether it's in progress, or whether it completed. Don't rely on guesswork.
  • Add logging around socket operations. Log the socket value, the target address, and the result of each connect(). That makes it obvious when you're hitting the same socket twice.
  • Write unit tests that deliberately try to reconnect a connected socket. Verify your code handles it gracefully (by closing and recreating) rather than crashing or leaking.

One more thing: if you're seeing this error in a third-party application or a Windows service, not your own code, you're stuck. You can't fix their bug. But you can sometimes work around it by restarting the service or the whole machine. That forces all sockets to close. It's a band-aid, but it works until the vendor patches their software.

That's the whole story. Close the socket before you reconnect. Do it every time. You won't see 0X00002748 again.

Was this solution helpful?