The gender is decided upon asking the doctor what the baby's gender is. The catch is that, in case of twins and triples, this will lock the first baby's gender, so if the doctor says it's a girl, it will be a girl regardless of how many apples you make your sim mother eat. After the first baby's gender is locked (in this case, girl), you can force the second twin's gender by eating it's specific fruit (apple for boys, watermelon for girls).
Interesting. It didn't work for my couple with their third child, after my female Sim's husband, who is a World Reowned Surgeon btw, said it was a boy, no amount of watermelons would change the gender. (I think she ate about 15).
Next I tried for mixed gender twins, the old fashioned way with her Doctor husband and the fruit, I have the latest patch but not Pets installed yet so I was taking no chances.
As soon as pregnancy was announced I had him determine the gender, a boy which was great as both my Sims wanted a boy. Because this happened late at night and they both tired I sent them both to bed straight after this and didn't have her start eating watermelon until the next day. She ate about 10-11 and he said it was a boy again, 2-3 watermelons later he still insisted it was a boy.
On the third day I had her scoff down about 20 watermelons (I was getting desperate by this stage, lol), I tried to have him determine the gender again but the option didn't show up for either of them. When she gave birth out came a boy and a girl!
The Mother had the Fertility Treatment LTR and watched the kids channel as well as listening to music mostly on the third day, both parents read the pregnancy books. Also both parents have the Family Orientated trait and the Mother has the Nurturing trait too, not sure if the latter make a differance though.
I'm just posting this to show that sometimes it works and sometimes it doesn't, lol. This is just my little experience, thought you might be interested.