The gender of the baby is determined at conception. The woman contributes the X chromosome and the male sperm can contribute either an X or a Y. Depending on what type of sperm unites with the egg is what determines the baby's gender.
In the 10th-12th weeks, the external genetalia are formed enough to determine the gender of the baby by diagnostic means.
Okay so you can't see it on a sonogram this early in most cases but at 9 weeks your baby has developed pretty much every part of every organ, its just a matter of maturing from that point on. i had a sonogram at 10 weeks and i could tell it was a boy just by looking at body structure. I dont know it was just a good guess. if you want to read articles about how your baby is developing just go to google or whatever search engine u use and search "pregnancy week 10" or whatever.
I had ultrasounds at 12 weeks with each of the boys...I knew then they were boys...there was NO doubt by what I saw on the screen. These three, however, are the exception to the general rule. It's more accurate to wait until about 20 weeks.