'Why is socket linger used after socket close?

I looked at documentation about this but shouldn't a socket linger be declared before the close so the program doesn't immediately abort before it knows what kind of shutdown to expect? Shouldn't linger be used first to explain to the socket close the shutdown should be one way or another? I'm looking at previously used code and the microsoft documentation but it just seemed odd to me.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms738547(v=vs.85).aspx

From this code, couldn't the socket immediately shutdown from the sockClose and the linger would be ignored? Code:

  self->socket = sockAccept(srvSocket)
  sockClose(srvSocket);
  sockDelay(self->socket, 1);
  sockBuffer(self->socket, pileSize + sizeof(messages));
  sockLinger(self->socket);


Solution 1:[1]

Yes, you must set linger options prior to closing a socket. Only during the close process the system kernel will look at the linger options and this changes the way how the socket is actually closed.

Please also refer to this answer for more details about lingering.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mecki