@@ -16,6 +16,14 @@ public class SocketConnector : ConnectorBase
1616 /// </summary>
1717 public IPEndPoint LocalEndPoint { get ; private set ; }
1818
19+ /// <summary>
20+ /// Gets or sets a value indicating whether the Nagle algorithm is disabled for the socket.
21+ /// When set to true, the socket will send data immediately without waiting to accumulate more data.
22+ /// This can improve performance for certain applications that require low latency, but may increase network traffic.
23+ /// The default value is true.
24+ /// </summary>
25+ public bool NoDelay { get ; set ; } = true ;
26+
1927 /// <summary>
2028 /// Initializes a new instance of the <see cref="SocketConnector"/> class with default settings.
2129 /// </summary>
@@ -34,6 +42,26 @@ public SocketConnector(IPEndPoint localEndPoint)
3442 LocalEndPoint = localEndPoint ;
3543 }
3644
45+ /// <summary>
46+ /// Configures the socket with the specified settings before connecting.
47+ /// This method can be overridden to customize the socket configuration, such as setting socket options or binding to a local endpoint.
48+ /// By default, it sets the NoDelay property and binds to the LocalEndPoint if it is specified.
49+ /// </summary>
50+ /// <param name="socket">The socket to configure.</param>
51+ protected virtual void ConfigureSocket ( Socket socket )
52+ {
53+ socket . NoDelay = NoDelay ;
54+
55+ var localEndPoint = LocalEndPoint ;
56+
57+ if ( localEndPoint != null )
58+ {
59+ socket . ExclusiveAddressUse = false ;
60+ socket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . ReuseAddress , 1 ) ;
61+ socket . Bind ( localEndPoint ) ;
62+ }
63+ }
64+
3765 /// <summary>
3866 /// Asynchronously connects to a remote endpoint using a TCP socket.
3967 /// </summary>
@@ -52,14 +80,8 @@ protected override async ValueTask<ConnectState> ConnectAsync(EndPoint remoteEnd
5280
5381 try
5482 {
55- var localEndPoint = LocalEndPoint ;
56-
57- if ( localEndPoint != null )
58- {
59- socket . ExclusiveAddressUse = false ;
60- socket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . ReuseAddress , 1 ) ;
61- socket . Bind ( localEndPoint ) ;
62- }
83+ ConfigureSocket ( socket ) ;
84+
6385#if NET5_0_OR_GREATER
6486 await socket . ConnectAsync ( remoteEndPoint , cancellationToken ) ;
6587#else
0 commit comments