Best place in code to setsockoptions from variables

A pointer on source modifications would be great.
In which function is the logical/best place to set custom socket options
after accept, from variable values set in nginx.conf, eg per location?
or, is it best to copy and modify an existing extension that uses that
hook. If so, which extension?
thanks