5. A drawer contains red and blue socks, with a total number not exceeding 2016. If two socks are randomly drawn, the probability that they are the same color is $\frac{1}{2}$. Then the maximum number of red socks in the drawer is . $\qquad$
5.990.

Let $x$ and $y$ be the number of red and blue socks in the drawer, respectively. Then
$$
\frac{x y}{\mathrm{C}_{x+y}^{2}}=\frac{1}{2} \Rightarrow (x-y)^{2}=x+y.
$$

Thus, the total number of socks is a perfect square.
Let $n=x-y$, i.e., $n^{2}=x+y$. Therefore, $x=\frac{n^{2}+n}{2}$.
Since $x+y \leqslant 2016$, we have $n \leqslant 44, x \leqslant 990$.
Hence, the maximum number of red socks in the drawer is 990.