What are the origins of the expression "Son of a Gun?"

Today, this expression is used in a joking manner, a term of happiness
or wonderment. It had a quite different meaning originally.

Once upon a time when women were allowed to live on naval ships, legend
has it varios rendevous would occur between these women and the ship's
crew near the main gun, with a canvas sheet screening alleged activities
from view.

Nine months later, if a male child was born of uncertain parentage, he
was entered into the ship's log as a "Son of a Gun."