CharlieP, willing to bet most 2 - 3 year olds know if they are a boy or a girl.
Please note that I didn't say they don't know if they are a boy or a girl, but that children that age do not have an awareness or understanding of sex as both a reproductive class and in terms of the different roles males and females have in that plus the associated stereotypes. The latter in particular happens at the end of the forth year and from that point onwards, children can develop rather rigid ideas of how boys and girls should be. These ideas however vary across space and time - sex stereotypes can differ hugely depending on which culture a child grows up in.
From about 18 months old, in a typical family where the child has someone caring for them in a mother and a father role, children begin to consciously differentiate between these two people and to observe differences between the role either parent figure takes up in caring for the child.
There is no transference from that onto the child's self, no identification with either sex, there is simply a child-mother and a child-father relationship. There is also no identification with peers or siblings of either sex at this stage.
Between 24 months and 42 months, the child begins to identify with one parent more than the other. This is an entirely flexible identification and has nothing to do with the child's actual sex. Most children who are looked after by their mothers for instance will first identify with the mother, but a child with a healthy attachment with the father will also identify with the father at some point.
This is an incredibly important time as children start to learn to love another person, beyond the self. Towards the upper end of this time is when the toddler's predilection for narcissism tends to lessen - empathy develops, a fledgling awareness of right and wrong and that others also have needs.
During this time the child frequently wishes to emulate the parent it identifies with, no matter whether this is the opposite-sex parent or not. This is not a sign of a rejection of the child's own sex, but merely the subconscious wish to become what it admires and loves. More than anything, at this stage, the child attempts to emulate, to be both, mother and father, without actually wishing to be male or female.
It is only after that stage, as I said, from 3 and a half years onwards that the child starts to develop a true understanding of its own sex, of sex role stereotypes, of the social coding that assigns behaviours, activities, personality traits, clothing and other matters of appearance preferentially to one sex over the other.
As long as these traits are not harmful to the child or others, it is vitally important to allow the child to choose freely from those, no matter what sex it is. The more rigid the environment it experiences, the more difficult it will be for the non-conforming child. For some non-conforming children growing up in households or social environments that rigidly police and enforce sex stereotypes, this is when conflicts arising from the child's own preferences vs societal expectations can damage the ongoing developmental process of the child's understanding of being male or female.
Depending on the child's familial circumstances, there are variations in this process, such as with single parents or same-sex couples, a child's illness or the loss of one or both parents.
The most damaging of those variations are where parent figures are abusive from an early age. As the child's wish to emulate a parent is rooted in its admiration and emerging love for them, abuse requently interferes with this particular aspect of the child's normal development.
It is therefore entirely unsurprising just how frequently the narratives of transsexual adults feature an abusive childhood.
That does not mean all who identify as trans were abused, of course, but it is one reason why those who were abused may later on identify as trans.