"Son of a gun" is a euphemism. From time to time, on departing from the shore, the crew of a ship would bring along a woman from said shore. Now, as things happen to turn out, the woman is inevitably rendered "with child", and because the potential sire could really be any number of them (in truth, probably only the integers 0 or 1), the father remains unknown. As the time to give birth dawns, the woman is sent to the gunnery, as it is usually the only private, clean place on a ship that sailors would want bodily fluids to spill. (!) The child is born, the peasants rejoice, and because the father is unknown, the child is said to be the "son of a gun". "Daughter of a gun" is also possible, but sailors are too manly to say that.