Davening on behalf of the child before and during the pregnancy:
Prior to intimacy: The husband and wife are to pray to G-d prior to the intimacy that they merit to have children who are successful Torah learners, have holy souls, are wealthy and healthy and righteous in all their ways. This prayer should be done prior to intimacy even during the months of pregnancy and certainly prior to her becoming pregnant.
Throughout the pregnancy: Throughout the days of the pregnancy, one should pray on behalf of their child that the child be God fearing and wise and be successful in his religious observance of Torah and Mitzvos and always distribute charity to the poor. Likewise, one should pray for the physical health of the child and that he not be sick and be carried to term.
Praying within 40 days from conception versus after 40 days: This prayer for the child to be wise and God-fearing may be recited throughout the pregnancy even past the first 40 days. Nonetheless, it is especially important to say this prayer throughout each of the first 40 days.
Davening for a boy [or girl]: Up until 40 days from the time of conception it is permitted for the parents to pray that the child be of a specific gender, whether a boy or girl. However, after 40 days from conception one may no longer Daven for the gender of the child due to it being a Tefilas Shav as the gender has already taken form by this time.
The Talmud’s directives of what to pray for by each period of the pregnancy: Within the first three days after intimacy, one should pray that the seed not spoil and that she conceive and become pregnant. From after the three days until the passing of 40 days from conception one should pray that the child be a male. From 40 days from conception until the passing of three months from conception one should pray that the child not be a sandal [i.e. that she not become pregnant with another child which can affect the figure of the child]. From three months from conception until six months one should pray that she does not miscarry. From the six-month until the ninth month one should pray that she have a healthy birth.
During candle lighting: Candle lighting time on Erev Shabbos is a most auspicious time for one to pray on behalf of one’s children. Thus, after lighting her Shabbos candles she is to pray on behalf of her yet to be born child. Some say the following Nussach of prayer “May it be your will that my children shine the world [ i.e. influence others] with Torah and mitzvahs and fear of heaven.”
The prayers of the wives of King David:
The wives of King David upon becoming pregnant with his child would may various pledges to charity and pray during the days of the pregnancy that the child be a great warrior and be physically beautiful and merit the kingship, although the children of these wives did not merit it. However, when Batsheva became pregnant she pledged to charity and prayed that her son be wise and God-fearing and merit prophecy, and God specifically heard her prayers and gave her son both what she prayed for and what she did not pray for, as King Solomon received wisdom more than any other man, and he merited also the monarchy. All this was because she focused her prayers on her son being a servant of God which is the main focus as opposed to praying for the monarchy which the otherwise made the main focus.
 See Shevach Habris 1:21; Nitei Gavriel Nidda 3 56:1-4
 Sh’lah Hakadosh Sha’ar Ha’osiyos Kedusha; Rabbeinu Yonah Brachos 5b; Niddah 70b; Zohar Chadash Bereishis 15b; Chesed Le’avraham Mayan Hasheini Nehar 65; Taharas Yisrael 240:92; Yalzu Chassidim p. 40b; Piskeiy Teshuvos 240:22 footnote 169; Sheyikadesh Atzmo 38:1
 Sh’lah ibid;
 Igeres Hateshuvah of Rabbeinu Yona 82 ; Elya Raba O.C. 230:1; Sh’lah Hakadosh Sha’ar Ha’osiyos Kedusha;
 Shevet Mussar 24; See Brachos 60a
 Elya Raba O.C. 230:1
 Kav Hayashar 17
 Michaber 230:1; Brachos 54a; 60a; Kaf Hachaim 230:2; Kitzur SHU”A 61:6; Poskim who prohibit a Tefilas Shav: Sefer Chassidim 794; Bechor Shur Shabbos 21; Kaf Hachaim 230:1; Or Hachaim in Chefetz Hashem Brachos 60a; Yabia Omer O.C. 35:11;
Opinion that may Daven past 40 days: Yerushlami Brachos 9:3; See Likkutei Sichos Vol. 24 p. 34
Background: The Mishneh states that one who Davens for a matter of the past is praying a Tefilas Shav, a prayer in vain. The Mishneh then continues and states, as recorded in the Shulchan Aruch, that one may only Daven for a specific desired gender of a child up until 40 days from conception, when it is still naturally possible for the gender to change. However, past 40 days one who Davens for a specific gender, then his prayer is defined as a Tefilas Shav, a prayer in vain, as he is asking G-d to change nature being that the gender is already set in place. The Gemara then explains that although Leah Davened to change the gender of her fetus from male to female, this was done within 40 days from conception, or perhaps was a mere miracle which we cannot rely upon. Now, while the wording in the Mishneh and Shulchan Aruch do not make clear whether there is an actual prohibition involved in reciting such a prayer, and it is possible to explain that there is no actual prohibition but a mere discouragement, practically, many Poskim rule that it is actually forbidden to Daven for a matter of change of nature which is a Tefilas Shav. To note, that according to the Yerushalmi, one may Daven for a male child up until the time of birth, although we do not rule this way.
 Brachos 60a; Se also Chida in Avodas Hakodesh a special prayer that he prescribed for the end of the three months, during the seventh month, and during the nine month.
 Igeres Hateshuvah of Rabbeinu Yona 80; see also Rabbeinu Bechayeh Yisro 19:3
 The reason: The reason for this is because a person’s prayer is more accepted above after the performance of a mitzvah. [Rabbeinu Yona ibid]
 M”A 263:11; Igeres Hateshuvah of Rabbeinu Yona 81; Rabbeinu Bechayeh Yisro 19:3; Mateh Moshe 417
 The reason: In merit of the Shabbos candles which is light one will merit to have children who can light the world. [Rabbeinu Bechayeh ibid]
 Igeres Hateshuvah of Rabbeinu Yona 82