jstedfast/MailKit · error · ArgumentException
The socket is not connected.
Error message
The socket is not connected.
What it means
Pop3Client.Connect(socket, host, port) validates the supplied Socket argument and throws this ArgumentException when socket.Connected is false. MailKit requires a pre-established TCP connection; it will not dial the endpoint itself when given a socket. The error means the caller passed a socket that was never connected or has been closed/disconnected.
Solutions
- Establish the connection first: socket.Connect(host, port) (or ConnectAsync) before passing it to Pop3Client.Connect.
- Check socket.Connected before handing the socket to the client and reconnect if false.
- If pooling sockets, validate/refresh them on checkout and discard dead ones.
- Create a fresh socket instead of reusing one from a closed session.
Example fix
// before
var socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect (socket, "pop.example.com", 995);
// after
var socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect ("pop.example.com", 995);
client.Connect (socket, "pop.example.com", 995); Defensive patterns
Strategy: validation
Validate before calling
if (socket == null || !socket.Connected)
throw new InvalidOperationException ("Socket must be connected before passing to Pop3Client.Connect"); Try / catch
try {
client.Connect (socket, host, port, options, cancellationToken);
} catch (ArgumentException ex) when (ex.ParamName == "socket") {
// socket not connected: reconnect and retry once
socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect (host, port);
client.Connect (socket, host, port, options, cancellationToken);
} Prevention
- Always call socket.Connect/ConnectAsync before handing the socket to Pop3Client
- Check socket.Connected on checkout when reusing pooled sockets
- Never reuse a socket after client.Disconnect(); create a fresh one
When it happens
Trigger: Passing a Socket created but never bound/connected; passing a socket whose remote peer already closed it (Connected becomes false); passing a closed socket after a failed or completed previous session.
Common situations: Custom connection-pooling code returning disconnected sockets; reusing a socket after Disconnect(); race where the socket died before being handed to Pop3Client; forgetting to call socket.Connect() first.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- The socket is not connected.
- The language code cannot be empty.
- The header field is invalid.
- One or more of the headers is invalid.
- The host name cannot be empty.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/f7fe1cf3cfcb7261.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Net/Pop3/Pop3Client.cs:1274
throw;
}
}
void CheckCanConnect (Stream stream, string host, int port)
{
if (stream == null)
throw new ArgumentNullException (nameof (stream));
CheckCanConnect (host, port);
}
void CheckCanConnect (Socket socket, string host, int port)
{
if (socket == null)
throw new ArgumentNullException (nameof (socket));
if (!socket.Connected)
throw new ArgumentException ("The socket is not connected.", nameof (socket));
CheckCanConnect (host, port);
}
/// <summary>
/// Establish a connection to the specified POP3 or POP3/S server using the provided socket.
/// </summary>
/// <remarks>
/// <para>Establishes a connection to the specified POP3 or POP3/S server using
/// the provided socket.</para>
/// <para>If the <paramref name="options"/> has a value of
/// <see cref="SecureSocketOptions.Auto"/>, then the <paramref name="port"/> is used
/// to determine the default security options. If the <paramref name="port"/> has a value
/// of <c>995</c>, then the default options used will be
/// <see cref="SecureSocketOptions.SslOnConnect"/>. All other values will use
/// <see cref="SecureSocketOptions.StartTlsWhenAvailable"/>.</para>
/// <para>Once a connection is established, properties such as
/// <see cref="AuthenticationMechanisms"/> and <see cref="Capabilities"/> will beView on GitHub (pinned to 9d3859a785)