Determining the sex of a person (in a legal sense) based on his or her genetic code would open up a whole can of worms. If we take that road, it won't end because more and more discoveries about human biology and genetics will be made. There are genetic disorders, which hopefully can be addressed, but we shouldn't build the foundation of our society on them.
If someone is XY but anatomically female, and the person's perception of gender is male, I would consider the person male. If a person is XY but anatomically female, and the person's perception of gender is female, I would consider the person female.
There are several genetic factors that make one male or female. Generally they are tied together on the Y chromosome, but if a person with an XY genotype is anatomically female or a person with an XX genotype is anatomically male, it would be clear that, at least for that individual, the genetic factors did not all go together. I would thus consider it reasonable to believe that those factors relating to perceived gender could be either male or female.